Hdu 6134 Battlestation Operational【容斥】
来源:互联网 发布:文豆php培训学校 编辑:程序博客网 时间:2024/05/21 21:33
Battlestation Operational
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 280 Accepted Submission(s): 151
>
> — Wookieepedia
In the story of the Rogue One, the rebels risked their lives stolen the construction plan of the Death Star before it can cause catastrophic damage to the rebel base. According to the documents, the main weapon of the Death Star, the Superlaser, emits asymmetric energy in the battlefield that cause photons to annihilate and burns everything in a single shot.
You are assigned the task to estimate the damage of one shot of the Superlaser.
Assuming that the battlefield is an
The figure below illustrates the damage caused to each cell for
Your should calculate the total damage to the battlefield. Formally, you should compute
where
Each line of the input, there is an integer
There are up to
1 2310
138110
题目大意:
F(n)=ΣΣi/j(gcd(i,j)==1);
让你求F(n)的值。
思路(思路代码参考自:http://blog.csdn.net/jaihk662/article/details/77338339):
我们设定g【i】表示每一行对结果贡献的价值。
那么我们不妨列出特殊的几列的值进行探讨:
现在列出所有位子的价值:
12345611/1 22/12/2 33/13/23/3 44/14/24/34/4 55/15/25/35/45/5 66/16/26/36/46/56/6其结果(上边是所有位子的价值,下边是每个位子真实贡献的价值):
我们假设要计算第六行的价值,我们需要去掉GCD(6,i)>1的所有位子的价值。
①第6行的贡献有:6/1,6/2,6/3,6/4,6/5,6/6
②第3行的贡献有:3/1,3/2,3/3.
③第2行的贡献有:2/1,2/2.
④第1行的贡献有:1/1.
我们将各式化简,有:
①第6行的贡献有:6/1,3/1,2/1,3/2,6/5,1/1
②第3行的贡献有:3/1,3/2,1/1.
③第2行的贡献有:2/1,1/1.
④第1行的贡献有:1/1.
然后我们考虑将各式子中1/1的部分去掉(因为gcd(i,i)一定大于1)有:
①第6行的贡献有:2 3 4 6,即:6/1,3/1,2/1,3/2,6/5
②第3行的贡献有:1 3.即:3/1,3/2
③第2行的贡献有:1 2.即:2/1
显然我们如果要计算第六行的实际贡献的话,需要将Gcd(6,i)>1的部分都去掉,那么显然要去掉的值有:3/1,2/1,3/2.
那么我们不难发现,此时我们对于第六行的实际贡献,只要将第二行和第三行的实际贡献去掉即可。
整个过程很容易理解,那么我们容斥一下就行。
设定G【i】表示我们第i行的全部贡献,设定F【i】表示我们第i行的实际贡献,那么就有:F【i】-=F【j】(j是i的因子)
容斥过程我们O(nlogn)就能够实现。
那么考虑如何计算G【i】,打表发现,我们每一行的全部贡献值是可以递推出来的,G【i】=G【i-1】+d【i-1】+1;
这里d【i】表示数字i因子数的个数。
那么预处理一下d【i】,再递推得到G【i】,然后容斥一下得到F【i】即可。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;#define ll __int64#define maxn 1000050#define mod 1000000007ll d[1050000];ll g[1050000];ll f[1050000];int main(){ for(int i=1;i<=maxn;i++) { for(int j=i;j<=maxn;j+=i) { d[j]++; } } for(int i=1;i<=maxn;i++) { g[i]=g[i-1]+d[i-1]+1; g[i]%=mod; } for(int i=1;i<=maxn;i++) { for(int j=i*2;j<=maxn;j+=i) { g[j]-=g[i]; g[j]=(g[j]%mod+mod)%mod; } } for(int i=1;i<=maxn;i++) { f[i]=f[i-1]+g[i]; f[i]=(f[i]%mod+mod)%mod; } ll n; while(~scanf("%I64d",&n)) { printf("%I64d\n",f[n]); }}
- Hdu 6134 Battlestation Operational【容斥】
- HDU 6134 Battlestation Operational
- HDU 6134 Battlestation Operational
- HDU 6134 Battlestation Operational (mobius +前缀和)
- hdu 6134 Battlestation Operational [反演]【数学】
- hdu 6134 Battlestation Operational mobius反演
- hdu 6134 Battlestation Operational 莫比乌斯反演
- HDU 6134 Battlestation Operational-莫比乌斯
- HDU 6134 Battlestation Operational(积性函数+莫比乌斯反演)
- 2017多校八 1002题 hdu 6134 Battlestation Operational 艾弗森约定 莫比乌斯函数 分块
- hdu 6134 Battlestation Operational (莫比乌斯反演+线性筛法+差分)
- HDU 6134 Battlestation Operational(莫比乌斯反演+线性筛)
- HDU 6134 Battlestation Operational 莫比乌斯反演 + 数学推导
- HDU 6134(2017 多校训练:Battlestation Operational(莫比乌斯反演))
- HDU 6134 Battlestation Operational 2017多校8 莫比乌斯反演
- HDU-2017 多校训练赛8-1002-Battlestation Operational
- HDU 6134 && 2017 多校训练:Battlestation Operational(莫比乌斯反演+积性函数)
- HDU 6134 Battlestation Operational(基本数论+莫比乌斯反演)——2017 Multi-University Training Contest
- SchemaManagementException----异常一只
- HDU 6133 Army Formations(2017 Multi-University Training Contest 8)
- svg个人小结
- string to integer
- poj-1330-Nearest Common Ancestors-LCA(RMQ)
- Hdu 6134 Battlestation Operational【容斥】
- Android studio gradle配置问题
- 机器学习学习笔记之二:决策树
- 第六届中国图学大会草记
- 带参数的宏定义
- 签名涂鸦手写板
- 欧拉回路定义(HDU
- JDK——Unsupported major.minor version 52.0问题解决
- Scala 语法