CF 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)=a1log⁡p1+a2log⁡p2+...+aklog⁡pk Where 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 33 in 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 .

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)=0exlogf(1)=0

exlogf(2)=2exlogf(2)=2

exlogf(3)=3exlogf(3)=3

exlogf(4)=2+2=4exlogf(4)=2+2=4

exlogf(5)=5exlogf(5)=5

exlogf(6)=2+3=5exlogf(6)=2+3=5

exlogf(7)=7exlogf(7)=7

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

exlogf(9)=3+3=6exlogf(9)=3+3=6

exlogf(10)=2+5=7exlogf(10)=2+5=7

exlogf(11)=11exlogf(11)=11

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

∑12i=1exlogf(i)=63∑i=112exlogf(i)=63

In the second sample:

exlogf(1)=0exlogf(1)=0

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

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

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

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

 

 

我的解题思路:首先看到sigma(1,n),然后发现每一项中会有重复的f(2),f(3)……,接着就想这解计算所有的f(2)的系数,发现是=n/2+n/2^2+n/2^3……,然后就是要找出所有的素数了,然后学习到了bitset的空间简化,重新练习了一遍埃氏筛法

接下来是别人的解题思路:转自https://blog.csdn.net/CSDNjiangshan/article/details/81536536

从 11 到 nn 内每个质数 pp 对答案的贡献为 ⌊np⌋+⌊np2⌋+⋯⌊npk⌋⌊np⌋+⌊np2⌋+⋯⌊npk⌋ ,其中 kk 为满足 pk≤npk≤n 的最大值,因此最后的答案就是:

 

∑i=1nf(pi)(⌊npi⌋+⌊np2i⌋+⋯⌊npki⌋)∑i=1nf(pi)(⌊npi⌋+⌊npi2⌋+⋯⌊npik⌋)

对 232232 取模,就是用 unsigned int 进行计算并让它们自然溢出。
最后是内存限制,如果我们用一个线性筛,就需要存每个数字是否是质数,如果用 bool 数组,3×1083×108 的 bool 数组需要 286MB286MB ,用 bitset 压位需要 2868≈35.75MB2868≈35.75MB ,如果用埃氏筛,就可以在筛的过程中跳过 22 和 33 的倍数,我们将删去 22 和 33 的倍数后连续的数字用对应的 1,2,...,n1,2,...,n 表示:

 

5577111113131717191923232525292931313535⋯⋯
11223344556677889910101111⋯⋯

可以发现数字与下标之间是 Index=⌊n3⌋Index=⌊n3⌋ 的关系,因此如果跳过 22 和 33 的倍数,可以减少 2323 的空间,最后只需要 35.753≈11.9MB35.753≈11.9MB 。

 

AC代码:注意要用unsigned,不然可能会超空间

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

#define mod 4294967296
bitset<100000003>bit;
unsigned n,a,b,c,d;


unsigned Get(unsigned x)
{
    unsigned temp1=(a*x*x*x+b*x*x+c*x+d)%mod;
    unsigned temp2=0;
    /*下面这么写是错的,因为y*x会超int
    unsigned y=x;
    while(n/y>0)
    {
        temp2+=n/y;
        y=y*x;
    }
    */
    for(unsigned i=1;i<=n/x;i*=x)
        temp2+=n/(i*x);
    return temp1*temp2%mod;
}

int main()
{

    cin>>n>>a>>b>>c>>d;
    unsigned int ans;
    ans=Get(2)+Get(3);
    bit.reset();
    for(unsigned i=5;i<=n;++i)
    {
        if(i%3==0||i%2==0)
            continue;
        if(bit[i/3]==0)
        {
            ans=(ans+Get(i))%mod;
            for(unsigned j=i;j<=n/i;++j)
            {
                if(i*j%3==0||i*j%2==0)
                    continue;
                bit[i*j/3]=1;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值