# Light OJ: 1340 – Story of Tomisu Ghost

2

Details about Prime Factorization of Factorial : http://goo.gl/qbNxfH

```
/*
If opportunity doesn't knock, build a door.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Success is how high you bounce when you hit bottom.
*/

#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout<<#x " = "<<(x)<<endl
#define VI               vector <int>
#define DBG              pf("Hi\n")
#define MOD              10000019
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf("%d",&a)
#define sfl(a)           scanf("%lld",&a)
#define sff(a,b)         scanf("%d %d",&a,&b)
#define sffl(a,b)        scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(i,a,b)      for(int i=a;i>=b;i--)
#define TEST_CASE(t)     for(int z=1;z<=t;z++)
#define PRINT_CASE       printf("Case %d: ",z)
#define CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

#define maxx 100005

bitset<maxx/2>vis;
vector<int>prime;

void sieve()
{
int x=maxx/2, y=sqrt(maxx)/2;
for(int i=1;i<=y;i++)
{
if(vis[i]==0)
{
for(int j=i*(i+1)*2;j<=x;j+=(2*i)+1)
vis[j]=1;
}
}
prime.pb(2);
for(int i=3;i<maxx;i+=2)
if(vis[i/2]==0)
prime.pb(i);
}

ll bigmod(ll n, ll pow, ll mod)
{
if(pow==0) return 1;
if(pow%2==0)
{
ll ret=bigmod(n,pow/2,mod)%mod;
return (ret*ret)%mod;
}
return ((n%mod)*(bigmod(n,pow-1,mod)%mod))%mod;
}

ll factorial_factor(int n, int t)
{
ll ans=1;

for(int i=0;i<SZ(prime) && prime[i]<=n;i++)
{
int nn=n;
ll sum=0;
while(nn/prime[i])
{
sum+=nn/prime[i];
nn/=prime[i];
}
if(sum>=t)
{
sum/=t;
ans*=bigmod(prime[i],sum,MOD);
ans%=MOD;
}
}
return ans;
}

int main()
{

///freopen("in.txt","r",stdin);
///freopen("out.txt","w",stdout);

sieve();
int t;
sf(t);
TEST_CASE(t)
{
int a,b;
sff(a,b);
ll xx=factorial_factor(a,b);
PRINT_CASE;
if(xx==1)
pf("-1\n");
else
pf("%lld\n",xx);
}

return 0;
}

```

# Light OJ: 1163 – Bank Robbery

0

```
/*

SOLUTION IDEA:
--------------

Let, the given number is X = A - B. Here, B = A/10.
So, A - A/10 = X
A - (A-A%10)/10 = X
10A - A + (A%10) = 10X
9A = 10X - K , let K = A%10
A = (10X - K)/9
A = X + (X - K)/9
For K equals to 0 to 9, if (X - K)%9 = 0, then A would be a solution.
If we get a solution for K = 0, then we would also get a solution for
K = 9 in this case. That means, if X%9 = 0, then there exists two solution.

*/

/*
If opportunity doesn't knock, build a door.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Success is how high you bounce when you hit bottom.
*/

#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout<<#x " = "<<(x)<<endl
#define VI               vector <int>
#define DBG              pf("Hi\n")
#define MOD              1000000007
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf("%d",&a)
#define sfl(a)           scanf("%lld",&a)
#define sff(a,b)         scanf("%d %d",&a,&b)
#define sffl(a,b)        scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(i,a,b)      for(int i=a;i>=b;i--)
#define TEST_CASE(t)     for(int z=1;z<=t;z++)
#define PRINT_CASE       printf("Case %d: ",z)
#define CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int main()
{

///freopen("in.txt","r",stdin);
///freopen("out.txt","w",stdout);

int t;
sf(t);
TEST_CASE(t)
{
ll a;
sfl(a);
bool test=0;
PRINT_CASE;
for(int i=9;i>=0;i--)
{
if((a-i)%9==0)
{
if(test) printf(" ");
printf("%lld",a+((a-i)/9));
test=1;
}
}
printf("\n");
}

return 0;
}

```

# Light OJ 1014 – Ifter Party

0

```
/*
If opportunity doesn't knock, build a door.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Success is how high you bounce when you hit bottom.
*/

#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout<<#x " = "<<(x)<<endl
#define VI               vector <int>
#define DBG              pf("Hi\n")
#define MOD              1000000007
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf("%d",&a)
#define sfl(a)           scanf("%lld",&a)
#define sff(a,b)         scanf("%d %d",&a,&b)
#define sffl(a,b)        scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(i,a,b)      for(int i=a;i>=b;i--)
#define TEST_CASE(t)     for(int z=1;z<=t;z++)
#define PRINT_CASE       printf("Case %d: ",z)
#define CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

bool vis[1000005];
vector<int>v;

void sieve()
{
for(int i=2;i<1000002;)
{
if(vis[i]==0)
{
v.pb(i);
for(int j=i+i;j<1000002;j+=i)
vis[j]=1;
}
for(++i;;i++) if(vis[i]==0) break;
}
}

int prime[1000000],cnt[1000000],k,l,p;

void gen_div(int n)
{
k=0;
int aa=sqrt(n)+1;
for(int i=0;v[i]<=aa;i++)
{
if(n%v[i]==0)
{
int cntt=0;
while(n%v[i]==0)
{
n/=v[i];
cntt++;
}
prime[k]=v[i];
cnt[k]=cntt;
k++;
}
}
if(n>1)
{
prime[k]=n;
cnt[k]=1;
k++;
}
}

vector<int>ans;

void gen_ans(int idx, int num)
{
if(idx>=k)
{
if(num>p) ans.pb(num);
return;
}
int val=1;
for(int i=0;i<=cnt[idx];i++)
{
gen_ans(idx+1,val*num);
val*=prime[idx];
}
}

int main()
{

///freopen("in.txt","r",stdin);
///freopen("out.txt","w",stdout);

sieve();

int t;
sf(t);
TEST_CASE(t)
{
sff(l,p);
gen_div(l-p);
ans.clear();
gen_ans(0,1);
sort(all(ans));
PRINT_CASE;
if(SZ(ans)==0)
pf("impossible\n");
else
{
pf("%d",ans[0]);
REP(i,1,SZ(ans)) pf(" %d",ans[i]);
pf("\n");
}

}

return 0;
}

```

# Light OJ 1197 – Help Hanzo

0

```
/*
If opportunity doesn't knock, build a door.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Success is how high you bounce when you hit bottom.
*/

#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout<<#x " = "<<(x)<<endl
#define VI               vector <int>
#define DBG              pf("Hi\n")
#define MOD              1000000007
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf("%d",&a)
#define sfl(a)           scanf("%lld",&a)
#define sff(a,b)         scanf("%d %d",&a,&b)
#define sffl(a,b)        scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(i,a,b)      for(int i=a;i>=b;i--)
#define TEST_CASE(t)     for(int z=1;z<=t;z++)
#define PRINT_CASE       printf("Case %d: ",z)
#define CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

#define maxx 100005
bitset<maxx>vis;

vector<int>prime;

void sieve()
{

int x=maxx/2, y=sqrt(maxx)/2;
for(int i=1;i<=y;i++)
{
if(vis[i])
{
for(int j=(i*(i+1))*2;j<=x;j+=(2*i)+1)
vis[j]=1;
}
}
prime.pb(2);
for(int i=3;i<maxx;i+=2)
if(vis[i/2]==0)
prime.pb(i);
}

int segmented_sieve(int a, int b)
{
vis=0;
if(b<2) return 0;
if(a<2) a=2;
int xx=sqrt((double)b)+1;
for(ll i=0;i<SZ(prime) && prime[i]<=xx;i++)
{
ll j=(a/prime[i]);
if(a%prime[i]!=0) j++;
j*=prime[i];
if(j==prime[i]) j+=prime[i];
for(;j<=b;j+=prime[i])
vis[j-a]=1;
}
int cnt=0;
for(ll i=a;i<=b;i++)
if(vis[i-a]==0) cnt++;
return cnt;
}

int main()
{

///freopen("in.txt","r",stdin);
///freopen("out.txt","w",stdout);
sieve();
int t;
sf(t);
TEST_CASE(t)
{
ll a,b;
sffl(a,b);
PRINT_CASE;
printf("%d\n",segmented_sieve(a,b));
}
return 0;
}

```

# SPOJ CPRIME – Prime Number Theorem

0

```
/*
If opportunity doesn't knock, build a door.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|S|.|S|.|R|.|A|.|S|.|A|.|M|.|K|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Success is how high you bounce when you hit bottom.
*/

#include <bits/stdc++.h>

#define pii              pair <int,int>
#define pll              pair <long long,long long>
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout<<#x " = "<<(x)<<endl
#define VI               vector <int>
#define DBG              pf("Hi\n")
#define MOD              1000000007
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf("%d",&a)
#define sfl(a)           scanf("%lld",&a)
#define sff(a,b)         scanf("%d %d",&a,&b)
#define sffl(a,b)        scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)      scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)     scanf("%lld %lld %lld",&a,&b,&c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define REP(i,a,b)       for(int i=a;i<b;i++)
#define RREP(i,a,b)      for(int i=a;i>=b;i--)
#define TEST_CASE(t)     for(int z=1;z<=t;z++)
#define PRINT_CASE       printf("Case %d: ",z)
#define CASE_PRINT       cout<<"Case "<<z<<": "
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1<<28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

#define maxx 100000008

bitset<maxx/2> prime;

vector<int>v;

void sieve()
{
int x=maxx/2, y=sqrt(maxx)/2;
for(int i=1; i<=y; i++)
{
if(prime[i]==0)
{
for(int j=(i*(i+1))*2; j<=x; j+=(2*i)+1)
prime[j]=1;
}
}
v.pb(2);
for(int i=3; i<maxx; i+=2)
if(prime[i/2]==0)
v.pb(i);
}

int main()
{

///freopen("in.txt","r",stdin);
///freopen("out.txt","w",stdout);

sieve();
int a;
while(sf(a) && a)
{
int cnt=upper_bound(all(v),a)-v.begin();
double aa=cnt;
double bb=a;
double ans=abs(aa-(bb/log(bb)))/aa;
printf("%.1f\n",ans*100);
}

return 0;
}

```