codeforce 1017 F. The Neutral Zone
F. The Neutral Zone
time limit per test
5 seconds
memory limit per test
16 megabytes
input
standard input
output
standard output
Notice: unusual memory limit!
After the war, destroyed cities in the neutral zone were restored. And children went back to school.
The war changed the world, as well as education. In those hard days, a new math concept was created.
As we all know, logarithm function can be described as:log(pa11pa22...pa2k)=a1logp1+a2logp2+...+aklogpklog(p1a1p2a2...pka2)=a1logp1+a2logp2+...+aklogpkWhere pa11pa22...pa2kp1a1p2a2...pka2 is the prime factorization of a integer. A problem is that the function uses itself in the definition. That is why it is hard to calculate.
So, the mathematicians from the neutral zone invented this:exlogf(pa11pa22...pa2k)=a1f(p1)+a2f(p2)+...+akf(pk)exlogf(p1a1p2a2...pka2)=a1f(p1)+a2f(p2)+...+akf(pk)
Notice that exlogf(1)exlogf(1) is always equal to 00.
This concept for any function ff was too hard for children. So teachers told them that ff can only be a polynomial of degree no more than 33in daily uses (i.e., f(x)=Ax3+Bx2+Cx+Df(x)=Ax3+Bx2+Cx+D).
"Class is over! Don't forget to do your homework!" Here it is:n∑i=1exlogf(i)∑i=1nexlogf(i)
Help children to do their homework. Since the value can be very big, you need to find the answer modulo 232232.
Input
The only line contains five integers nn, AA, BB, CC, and DD (1≤n≤3⋅1081≤n≤3⋅108, 0≤A,B,C,D≤1060≤A,B,C,D≤106).
Output
Print the answer modulo 232232.
思路:
每個素數p對答案的貢獻是 n/(p^1)+n/(p^2)+n/(p^3)....
即 f(p)*{n/(p^1)+n/(p^2)+n/(p^3)....} p取遍2~n中的所有素數。
資料有3e8,只用bitset優化還是會超限,因為不能被2,3整除的數除以3
等於它排列的下標值:如前20個數中不能被2,3整除的有:
a[1]=5,a[2]=7,a[3]=11,a[4]=13,a[5]=17,a[6]=19。
有a[1]/3=1,a[2]/3=2,a[3]/3=3,a[4]/3=4,a[5]/3=5,a[6]/3=6。
因此在用陣列標記素數的時候可以把是2或3的倍數的數剔除掉。
這樣就可以開一個1e8的bitset。
程式碼:
#include<bits/stdc++.h>
using namespace std;
#define uint unsigned int
const int maxn=1e8+10;
bitset<maxn>G;
uint n,A,B,C,D;
uint get_ans(uint x)
{
uint ans=0;
for(uint i=1;i<=n/x;i*=x) ans+=n/(i*x);
return ans*(A*x*x*x+B*x*x+C*x+D);
}
int main()
{
scanf("%u%u%u%u%u",&n,&A,&B,&C,&D);
uint ans=get_ans(2)+get_ans(3);
for(uint i=5;i<=n;i++)
{
if(i%2==0||i%3==0) continue;
if(!G[i/3])
{
ans+=get_ans(i);
for(int j=i;j<=n;j+=i)
{
if(j%2==0||j%3==0) continue;
G[j/3]=1;
}
}
}
printf("%u\n",ans);
return 0;
}