Codeforces 1017F 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+…+aklogpk
Where pa11pa22…pa2k

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)

Notice that exlogf(1)
is always equal to 0

.

This concept for any function f
was too hard for children. So teachers told them that f can only be a polynomial of degree no more than 3 in daily uses (i.e., f(x)=Ax3+Bx2+Cx+D

).

“Class is over! Don’t forget to do your homework!” Here it is: n∑i=1exlogf(i)

Help children to do their homework. Since the value can be very big, you need to find the answer modulo 232

.
Input

The only line contains five integers n
, A, B, C, and D (1≤n≤3⋅108, 0≤A,B,C,D≤106

).
Output

Print the answer modulo 232

.
Examples
Input
Copy

12 0 0 1 0

Output
Copy

63

Input
Copy

4 1 2 3 4

Output
Copy

136

Note

In the first sample:

exlogf(1)=0

exlogf(2)=2

exlogf(3)=3

exlogf(4)=2+2=4

exlogf(5)=5

exlogf(6)=2+3=5

exlogf(7)=7

exlogf(8)=2+2+2=6

exlogf(9)=3+3=6

exlogf(10)=2+5=7

exlogf(11)=11

exlogf(12)=2+2+3=7

∑12i=1exlogf(i)=63

In the second sample:

exlogf(1)=0

exlogf(2)=(1×23+2×22+3×2+4)=26

exlogf(3)=(1×33+2×32+3×3+4)=58

exlogf(4)=2×exlogf(2)=52

∑4i=1exlogf(i)=0+26+58+52=136

有篇博客写的非常好,我就不班门弄斧了(其实懒得写了)

传送门

不过这确实是道好题

学习到了以下几点:

1.取模 2 32 实际上就是用unsigned int直接计算让他自然溢出

2.使用bitset进行位压,实际上这是我第二次做到用bitset的题

3.埃式筛如果把2和3的倍数都去掉的话,剩下的数按照从1-n排列,每个数的标号相当于这个数除3,这样大大节省了内存开销

code:

#include <bits/stdc++.h>
using namespace std;
unsigned int n,a,b,c,d;
unsigned int ans;
bitset<100000001> bit;
inline unsigned int f(unsigned int x){
    return a * x * x * x + b * x * x + c * x + d;
}
inline unsigned int Get(unsigned int x){
    unsigned int ret = f(x);
    unsigned int tmp = 0;
    for(unsigned int i = 1; i <= n / x; i *= x){
        tmp += n / (i * x);
    }
    return ret * tmp;
}
int main(){
    while(~scanf("%u%u%u%u%u",&n,&a,&b,&c,&d)){
        ans = Get(2) + Get(3);
        bit.reset();
        for(unsigned int i = 5; i <= n; i++){
            if(i % 2 == 0 || i % 3 == 0){
                continue;
            }
            if(bit[i/3] == 0){
                ans += Get(i);
                for(unsigned int j = i; j <= n; j += i){
                    if(j % 2 == 0 || j % 3 == 0) continue;
                    bit[j/3] = 1;
                }
            }
        }
        printf("%u\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82413686