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\) ...
随机推荐
- android shape的用法总结
参考代码: <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corner ...
- Spring IOC(通过实例介绍,属性与构造方法注入)
概括说明:下面通过实例介绍下属性方法注入.构造方法注入 1.源码结构图 2.代码介绍 (1).Dao接口 :UserDAO (2).Dao接口实现:UserDAOImpl (3).实体类:User ( ...
- TP框架的修改,删除
先把数据库的素具显示出来 public function xiugai() { $code= "n001";//修改的主键值 $n = M("nation"); ...
- SDOI2012 Round1 day2 象棋(chess)解题报告
本题的难点是“移动过程中不能出现多颗棋子同时在某一格的情况”. 事实上,可以忽略此条件,因为棋子是相同的,我们可以用合法的等效方案替代一棋子越过另一棋子的情况:A.B.C三格,A能在一步走到B,B也能 ...
- 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2
P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...
- zipkin环境搭建
基于jdk1.8 下载zipkin jar包 用wget下载zipkin官方最新jar包 wget -O zipkin.jar 'https://search.maven.org/remote_con ...
- 【译】理解Spring MVC Model Attribute 和 Session Attribute
作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...
- [note]可持久化Trie
可持久化Trie 参考可持久化线段树的思想,修改的时候先直接复制,再对需要修改的点新建节点 可持久化Trie也是同样的做法,假设现在需要在原本Trie的基础上插入一个字符串 先把上个Trie的对应节点 ...
- Linux中假设系统丢失了ls命令
假设系统丢失了ls命令,如何修复: 1.查询ls命令属于那个rpm包[root@localhost ~]# rpm -qf /bin/lscoreutils-8.4-43.el6.x86_64 2.挂 ...
- Linux c编程:I/O多路复用之select
一般我们在写socet程序的时候调用的accept,recv等操作都是阻塞型的.意思就是如果我们一直收不到数据那么则会被阻塞.所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待 ...