Codeforces 1017F The Neutral Zone 数论
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1017F.html
题目传送门 - CF1017F
题意
假设一个数 $x$ 分解质因数后得到结果 $x=p_1^{a_1}p_2^{a_2}\cdots p_k^{a_k} $
定义 $\text{exlog}_f(x) = a_1 f(p_1) + a_2 f(p_2) + ... + a_k f(p_k)$
给定 $A,B,C,D$ 表示 $f(x)=Ax^3+Bx^2+Cx+D$
求 $\sum_{i=1}^n \text{exlog}_f(i)$ 。
$n\leq 3\times 10^8,\ \ A,B,C,D\leq 10^6,\ \ $ 答案对于 $2^{32}$ 取模。
题解
考虑一个素数 $p$ 对答案的贡献。定义 $cnt(条件)$ 为 $1$~$n$ 中满足条件的数的个数。
显然,一个素数对答案的贡献是: $\sum_{i=1}^{\infty} cnt((x\mod {p^i}=0)\ and\ (x\mod {p^{i+1}}\neq 0))\times i \times f(p_i)$ 。
由于质数的个数大约在 $\cfrac{n}{\log(n)}$ 数量级,所以我们可以一个 $log$ 求上面的东西。
接下来的问题就是如何快速得到所有质数。
考虑一个大于 $3$ 的质数对于 $6$ 取模只可能是 $1$ 或 $5$ 。
这样,我们就把可能的范围缩小了 $3$ 倍。
空间限制很小,我们需要 32 位压位,然后之前又压了 $3$ 倍,空间刚好卡进 16MB。
然后就一边暴力筛出素数,一边求当前素数对于答案的贡献。
我的写法细节比较多,不推荐。可以考虑损失一点常数来做,会好写一些。
代码
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned uint;
LL read(){
LL x=0;
char ch=getchar();
while (!isdigit(ch))
ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x;
}
uint n,A,B,C,D,ans=0;
uint fff[3333533];
// 6 - 1 5
uint F(uint v){
return A*v*v*v+B*v*v+C*v+D;
}
void update(uint p){
uint m=n,d=m/p,dd,i=0;
while (p<=d){
i++;
dd=d/p;
ans+=i*(d-dd)*F(p);
d=dd;
}
ans+=(i+1)*d*F(p);
}
int main(){
memset(fff,0,sizeof fff);
n=read(),A=read(),B=read(),C=read(),D=read();
for (uint i=2,f=0,k=0;i<=n;){
uint id=k<<1;
if (f==5)
id--;
if (i>5&&((fff[id>>5]>>(id&31))&1))
;
else {
// printf("%d\n",i);
update(i);
if (i>=5){
uint ii=i,ff=f,kk=k,iid,v1=4,v2=2;
if (f==5)
swap(v1,v2);
while (1){
if (ff==1)
ii+=v1*i,ff=5;
else
ii+=v2*i,ff=1;
if (ii>n)
break;
// printf("%u %u %u\n",ii,ff,kk);
kk=(ii+1)/6;
iid=kk<<1;
if (ii%6==5)
iid--;
fff[iid>>5]|=1<<(iid&31);
}
}
}
if (i<5){
if (i==2)
i=3;
else
i=5,f=5,k=1;
}
else if (f==1)
i+=4,f=5,k++;
else
i+=2,f=1;
}
printf("%u",ans);
return 0;
}
/*
100 0 0 0 1
*/
Codeforces 1017F The Neutral Zone 数论的更多相关文章
- CodeForces - 1017F. The Neutral Zone (数学+Bitset存素数+素数筛优化)
Notice: unusual memory limit! After the war, destroyed cities in the neutral zone were restored. And ...
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
- [CodeForces - 1225D]Power Products 【数论】 【分解质因数】
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
- Codeforces 833A The Meaningless Game - 数论 - 牛顿迭代法 - 二分法
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. T ...
- Codeforces 837E Vasya's Function - 数论
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...
- Codeforces 837D Round Subset - 动态规划 - 数论
Let's call the roundness of the number the number of zeros to which it ends. You have an array of n ...
- Codeforces 955C - Sad powers(数论 + 二分)
链接: http://codeforces.com/problemset/problem/955/C 题意: Q次询问(1≤Q≤1e5),每次询问给出两个整数L, R(1≤L≤R≤1e18),求所有符 ...
随机推荐
- [转]MingGW64 多个版本区别(silj, seh)
1 引言 在https://sourceforge.net/projects/mingw-w64上会看到MinGW有各种版本如下,但区别在于哪儿呢 MinGW-W64 GCC-8.1.0 x86_64 ...
- IOS 颜色的宏定义
#define RGB(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a] self.vie ...
- 【MySql】删除操作
删除表内数据,用 delete.格式为: delete from 表名 where 删除条件; 实例:删除学生表内姓名为张三的记录. delete from student where T_name ...
- Uiautomator - 6.0 以上权限受限问题
问题:在android studio中使用UiAutomator 2.0 编写测试用例时,要实现截图(非命令方式),写入文件时出现权限被拒绝的提示.例如: java.io.FileNotFoundEx ...
- 【python】打印函数调用栈
traceback.print_stack()
- Java 并发类
java.util.concurrent包里 提供了一批线程安全的类 一. java.util.concurrent.atomic java.util.concurrent.atomic包里的原子处理 ...
- HTML&javaSkcript&CSS&jQuery&ajax(四)
一.HTML创建响应设计 Responsive Web Design 可以改变尺寸传递网页,对于平板和移动设备是必须的 1.<!DOCTYPE html><html lang=&qu ...
- Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。
解决方法: 定义了两个 Vue.component 在 el 中使用的时候要用 双标签, 用单表标签的时候,只会显示第个 组件间 这样写只显示 welcome-button 组件 <welcom ...
- 从认识面向对象到构造函数的标准写法(构造函数的继承、多态、ECMA6中新代替语法class) - 下
笔记一个包含:认识面向对象.构造函数的封装.继承.多态.ECMA6中新代替语法class 下:包括构造函数的继承.多态.ECMA6中新代替语法class 构造函数的继承 从父一级延续下来的属性和功能( ...
- Python字典(Dictionary)
Python中字典与类表类似,也是可变序列,不过与列表不同,他是无序的可变序列,保存的内容是以键 - 值对的形式存放的.类似我们的新华字典,他可以把拼音和汉字关联起来,通过音节表可以快速的找到想要的字 ...