CodeForces - 1017F. The Neutral Zone (数学+Bitset存素数+素数筛优化)
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:
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:
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:
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
12 0 0 1 0
63
4 1 2 3 4
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存素数+素数筛优化)的更多相关文章
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces 1017F The Neutral Zone 数论
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1017F.html 题目传送门 - CF1017F 题意 假设一个数 $x$ 分解质因数后得到结果 $x=p ...
- 【CF1017F】The Neutral Zone(Bitset,埃氏筛)
题意: 思路:From https://blog.csdn.net/CSDNjiangshan/article/details/81536536 #include<cstdio> #inc ...
- 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 ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...
随机推荐
- elasticsearch从入门到出门-05-集群之踩坑
自己搭的集群, 设备: win10 + ubuntu 16 的虚拟机一个: 下载的版本:elasticsearch-5.2.0 win10 解压下就好了,不说了. ubuntu 上,我想说多了都是泪! ...
- 6.2-SingletonBeanRegistry-DefaultSingletonBeanRegistry
SingletonBeanRegistry package org.springframework.beans.factory.config; public interface SingletonBe ...
- Delphi窗体研究,留个爪,以后回来研究
Delphi - 窗体创建过程 来自大富翁. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- [luogu3393]逃离僵尸岛
[luogu3393]逃离僵尸岛 luogu 先把被禁止的点和新建的虚点n+1连0边 跑最短路,dis<=s的点价格为Q,否则为P, 再建图跑最短路 #define ll long long # ...
- PHP安全编程之php.ini配置
1.register_globals=On <?php//ex1.php if(check_admin()) { $is_admin=true; } if($is_admin) { ...
- docker centos yum 源
aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...
- 运行“cordova build android” - 无法找到属性android:fontVariationSettings和android:ttcIndex
:app:processArm64DebugResourcesC:\Users\xfcao\.gradle\caches\transforms-1\files-1.1\xwalk_core_libra ...
- 第11条:用zip函数同时遍历两个迭代器
核心知识点: (1)内置的zip函数可以平行地遍历多个迭代器. (2)python3中地zip相当于生成器,会在遍历过程中逐次产生元祖.而python2中地zip则是直接把这些元祖完全生成好,并一次性 ...
- mapreduce源码解析以及优化
http://blog.csdn.net/wisgood/article/details/8793483 优化 http://blog.csdn.net/wisgood/article/details ...
- Python lambda 匿名函数
lambda [arg1[, arg2, ... argN]]: expression Python使用lambda关键字创造匿名函数.所谓匿名,意即不再使用def语句这样标准的形式定义一个函数.这种 ...