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),求所有符 ...
随机推荐
- 模拟电路学习之NMOS开关电路1
- WinCE平台的程序编译到Win32平台下运行
最近做的项目中,有一个在WinCE平台上跑的程序,后来随着项目的发展,要求此程序在PC上也能跑.感谢VS 2005提供的多平台支持,只需要几分钟就可以解决这个问题,方法很简单,下面是我处理的过程. 1 ...
- django.db.utils.OperationalError: (1049, "Unknown database 'djangodb'")
DATABASES = { 'default': { 'ENGINE':'django.db.backends.mysql', 'NAME': 'mysql', 'USER':'root', 'PAS ...
- python字符串,列表常用操作
24天养成一个好习惯,第五天! 一.字符串需要掌握的操作 1.取值(索引取值)需要注意的是只能取,不能改 msg = 'hello world' print(msg[4]) 2.切片(顾头不顾尾) m ...
- Docker快速搭建WordPress博客网站
WordPress WordPress是一个非常著名的PHP编写的博客平台,发展到目前为止已经形成了一个庞大的网站平台系统.在WP上有规模庞大的插件和主题,可以帮助我们快速建立一个博客甚至网站. 在W ...
- 使用gulp-babel转换Es6出现exports is not defined 问题
//问题描述:当使用import导入模块时,出现exports is not defined //1.安装插件 npm install --save-dev babel-plugin-transfor ...
- 中介模型以及优化查询以及CBV模式
一.中介模型:多对多添加的时候用到中介模型 自己创建的第三张表就属于是中介模型 class Article(models.Model): ''' 文章表 ''' title = models.Char ...
- hdu5015构造转移矩阵
/* 构造转移矩阵: 先推公式: 首先是第0行:A[0][j+1]=A[0][j]*10+3 1-n行: A[i][j+1]=A[i][j]+A[i-1][j+1]=... =A[i][j]+A[i- ...
- Nginx详解二十七:Nginx架构篇之安全篇
1.常见的恶意行为:爬虫行为和恶意抓取.资源盗用 解决方案: 基础防盗链功能:不让恶意用户能轻易爬去网站对外数据 secure_link_module模块:对数据安全性提高,加密验证和失效性,适合核心 ...
- C++ 关于ShowWindow()的疑问
IDE: Code::Blocks 16.01 操作系统:Windows 7 x64 最初的代码,目的是为了隐藏窗口出现在任务栏上的图标. #include <windows.h> usi ...