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:

∑i=1nexlogf(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
12 0 0 1 0
Output
63
Input
4 1 2 3 4
Output
136

题意:给定函数Fx=A*x^3+B*x^2+C*x+D; 而函数Fx=Fa1+Fa2+...,当且仅当x=p1^a1+p2^a2+...

思路:很显然我们是算关于每个素数p的函数,Fp。然后乘其贡献次数nump。 Fp可以算,nump=N/p+N/p/p+N/p/p/p+....

N以内的素数个数约有N/lnN个,所以算每个素数的nump复杂度为O(NlglgN),可以搞。那么现在的关键就是在16M,5s的空间和时间里筛出3e8的素数。

显然压空间可以用bitset,bitset存1e8的空间只需要12.5M。再利用2,3以外的素数==6x+-1,压缩一下就搞定了。 以下代码筛3e8的素数只需要250ms。

void prime()
{
add(); add(); //单独考虑
for(UI i=,d=;i<=N;i+=d,d=-d) {
if(!p[i/]){
add(i); if(i>N/i) continue;
for(UI j=i*i,v=d;j<=N;j+=i*v,v=-v) p[j/] = ;
}
}
}

所以,搞定! 当然,也可以用区间筛法,一段一段的搞定。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned int UI;
UI ans,N,A,B,C,D;
bitset<>p;
inline void add(UI x) {
UI f=A*x*x*x+B*x*x+C*x+D;
for(UI t=N;t;t/=x) ans+=t/x*f;
}
void prime()
{
add(); add(); //单独考虑
for(UI i=,d=;i<=N;i+=d,d=-d) {
if(!p[i/]){
add(i); if(i>N/i) continue;
for(UI j=i*i,v=d;j<=N;j+=i*v,v=-v) p[j/] = ;
}
}
}
int main() {
scanf("%u%u%u%u%u",&N,&A,&B,&C,&D);
prime();
printf("%u\n", ans);
return ;
}

CodeForces - 1017F. The Neutral Zone (数学+Bitset存素数+素数筛优化)的更多相关文章

  1. Codeforces 1017F The Neutral Zone (看题解)

    这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces 1017F The Neutral Zone 数论

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1017F.html 题目传送门 - CF1017F 题意 假设一个数 $x$ 分解质因数后得到结果 $x=p ...

  3. 【CF1017F】The Neutral Zone(Bitset,埃氏筛)

    题意: 思路:From https://blog.csdn.net/CSDNjiangshan/article/details/81536536 #include<cstdio> #inc ...

  4. Codeforces 789e The Great Mixing (bitset dp 数学)

    Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th typ ...

  5. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  6. Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

    题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...

  7. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  8. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  9. Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...

随机推荐

  1. 在Linux先显示文件

    cat:从第一行开始显示文件内容. tac:从最后一行开始显示内容. nl:显示的时候带行号. more:一页一页显示. less:同more,可以向上翻页. head:显示文件前几行. head - ...

  2. Android之——清理手机SD卡缓存

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47375595 眼下,市场上非常多Android手机软件都具有清理SD卡缓存的功能, ...

  3. Root(hdu5777+扩展欧几里得+原根)

    Root                                                                          Time Limit: 30000/1500 ...

  4. 1、Codevs 必做:2833、1002、1003、2627、2599

    2833 奇怪的梦境  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...

  5. 反素数ant(数学题)

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2872  Solved: 1639[Submit][St ...

  6. POJ 1694 An Old Stone Game【递归+排序】

    链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  7. Numerical Differentiation 数值微分

    zh.wikipedia.org/wiki/數值微分 数值微分是数值方法中的名词,是用函数的值及其他已知资讯来估计一函数导数的算法. http://mathworld.wolfram.com/Nume ...

  8. ASP获取上月本月下月的第一天和最后一天

    上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...

  9. 次小生成树Tree

    次小生成树Treehttps://www.luogu.org/problemnew/show/P4180 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正 ...

  10. python cookbook第三版学习笔记十八:可由用户修改的装饰器

    定义一个属性可由用户修改的装饰器: 在前面的介绍中使用装饰器来包装函数,这一章来介绍下如何让用户调整装饰器的属性. 首先来看下代码: from functools import wraps,parti ...