Problem link : http://lightoj.com/volume_showproblem.php?problem=1289
Tutorial : https://nlognblog.wordpress.com/2015/12/01/light-oj1289-lcm-from-1-to-n/
Hint :
Solution :
#include <bits/stdc++.h>
#define ll unsigned
using namespace std;
const int MAX = 100000000; // 10^8
const int LMT = 10000; // sqrt(MAX)
int _c[(MAX>>6)+1];
int primes[5761482];
#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
ll mnt=0;
void sieve()
{
int x=0;
for (int i = 3; i <= LMT; i += 2)
if (!IsComp(i))
for (int j = i*i; j <= MAX; j += i+i)
SetComp(j);
primes[x++]=2;
mnt++;
for (int i=3; i <= MAX; i += 2)
if (!IsComp(i))
{
primes[x++]=i;
mnt++;
}
}
ll store[5761482];
void precal()
{
store[0]=2;
for(ll i=1; i<mnt; i++)
{
store[i]=store[i-1]*primes[i];
}
}
ll cal(ll n)
{
ll temp,ret=1;
for(ll i=0; primes[i]*primes[i]<=n; i++)
{
temp=n;
temp=temp/primes[i];
while(temp>=primes[i])
{
temp=temp/primes[i];
ret=ret*primes[i];
}
}
return ret;
}
int main()
{
ll n,val,sq,q,rs,cnt,ans,t,ca=1;
sieve();
precal();
scanf("%u",&t);
while(t--)
{
scanf("%u",&n);
ans=cal(n);
val=upper_bound(primes,primes+mnt,n)-primes;
val--;
ans=ans*store[val];
printf("Case %u: %u\n",ca,ans);
ca++;
}
}
Tutorial : https://nlognblog.wordpress.com/2015/12/01/light-oj1289-lcm-from-1-to-n/
Hint :
- When a problem says that you have to do modulo 2^32 (and it requires only addition, subtraction and multiplication) then just take them as unsigned integers and do regular operations. Just think that the compiler uses 32 bit operations, and if it overflows, then the most significant bits will be lost but the least significant 32 bits will be correct, which is equivalent to modulo 2^32.
Solution :
#include <bits/stdc++.h>
#define ll unsigned
using namespace std;
const int MAX = 100000000; // 10^8
const int LMT = 10000; // sqrt(MAX)
int _c[(MAX>>6)+1];
int primes[5761482];
#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
ll mnt=0;
void sieve()
{
int x=0;
for (int i = 3; i <= LMT; i += 2)
if (!IsComp(i))
for (int j = i*i; j <= MAX; j += i+i)
SetComp(j);
primes[x++]=2;
mnt++;
for (int i=3; i <= MAX; i += 2)
if (!IsComp(i))
{
primes[x++]=i;
mnt++;
}
}
ll store[5761482];
void precal()
{
store[0]=2;
for(ll i=1; i<mnt; i++)
{
store[i]=store[i-1]*primes[i];
}
}
ll cal(ll n)
{
ll temp,ret=1;
for(ll i=0; primes[i]*primes[i]<=n; i++)
{
temp=n;
temp=temp/primes[i];
while(temp>=primes[i])
{
temp=temp/primes[i];
ret=ret*primes[i];
}
}
return ret;
}
int main()
{
ll n,val,sq,q,rs,cnt,ans,t,ca=1;
sieve();
precal();
scanf("%u",&t);
while(t--)
{
scanf("%u",&n);
ans=cal(n);
val=upper_bound(primes,primes+mnt,n)-primes;
val--;
ans=ans*store[val];
printf("Case %u: %u\n",ca,ans);
ca++;
}
}