【BZOJ3944/4805】Sum/欧拉函数求和 杜教筛
【BZOJ3944】Sum
Description
Input
Output
Sample Input
1
2
8
13
30
2333
Sample Output
2 0
22 -2
58 -3
278 -3
1655470 2
题解:
当i等于1时就是答案,剩余的部分递归算下去就行了(先预处理出1000000以内的答案,其余的答案要用map保存)
粘自http://blog.csdn.net/skywalkert/article/details/50500009
求莫比乌斯函数的前缀和类似,从
开始推就好了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <utility>
#define MP(A,B) make_pair(A,B)
using namespace std;
const int m=3000000;
typedef long long ll;
int n,num;
ll phi[m+10],mu[m+10],sp[m+10],sm[m+10],pri[m+10];
bool np[m+10];
typedef pair<ll,ll> pll;
map<ll,pll> mp;
pll dfs(ll x)
{
if(x<=m) return MP(sp[x],sm[x]);
if(mp.find(x)!=mp.end()) return MP(mp[x].first,mp[x].second);
ll rp=x*(x+1)>>1,rm=1,i,last;
for(i=2;i<=x;i=last+1)
{
last=x/(x/i);
pll tmp=dfs(x/i);
rp-=tmp.first*(last-i+1);
rm-=tmp.second*(last-i+1);
}
mp[x]=MP(rp,rm);
return MP(rp,rm);
}
int main()
{
int T,i,j;
scanf("%d",&T);
phi[1]=sp[1]=mu[1]=sm[1]=1;
for(i=2;i<=m;i++)
{
if(!np[i]) pri[++num]=i,phi[i]=i-1,mu[i]=-1;
sp[i]=sp[i-1]+phi[i],sm[i]=sm[i-1]+mu[i];
for(j=1;j<=num&&i*pri[j]<=m;j++)
{
np[i*pri[j]]=1;
if(i%pri[j]==0)
{
phi[i*pri[j]]=phi[i]*pri[j];
mu[i*pri[j]]=0;
break;
}
mu[i*pri[j]]=-mu[i];
phi[i*pri[j]]=phi[i]*(pri[j]-1);
}
}
while(T--)
{
ll a;
scanf("%lld",&a);
pll tmp=dfs(a);
printf("%lld %lld\n",tmp.first,tmp.second);
}
return 0;
}
【BZOJ3944/4805】Sum/欧拉函数求和 杜教筛的更多相关文章
- 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛
bzoj3944 题目描述 输入 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 输出 一共T行,每行两个用空格分隔的数ans1,ans2 样例输 ...
- BZOJ4805: 欧拉函数求和(杜教筛)
4805: 欧拉函数求和 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 614 Solved: 342[Submit][Status][Discus ...
- BZOJ 4805: 欧拉函数求和 杜教筛
https://www.lydsy.com/JudgeOnline/problem.php?id=4805 给出一个数字N,求sigma(phi(i)),1<=i<=N https://b ...
- LOJ6686 Stupid GCD(数论,欧拉函数,杜教筛)
做题重心转移到 LOJ 了. 至于为什么,如果你知道“……”的密码,就去看吧. LOJ 上用户自创题大多数都不可做,今天看到个可做题(而且还是个水题),就来做了一发. 明显枚举立方根.(以下令 $m= ...
- 51 NOD 1239 欧拉函数之和(杜教筛)
1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...
- 【51nod】1239 欧拉函数之和 杜教筛
[题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...
- 51nod1244 欧拉函数之和 杜教筛
和上一题差不多,一个是μ*I=e,一个是φ*I=Id 稍改就得到了这题的代码 (我会告诉你我一开始逆元算错了吗) #include <bits/stdc++.h> #define MAX ...
- [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)
[51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1Nμ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...
- 【BZOJ4805】欧拉函数求和(杜教筛)
[BZOJ4805]欧拉函数求和(杜教筛) 题面 BZOJ 题解 好久没写过了 正好看见了顺手切一下 令\[S(n)=\sum_{i=1}^n\varphi(i)\] 设存在的某个积性函数\(g(x) ...
随机推荐
- C++ 和 java 使用 AES CBC 128 加解密
Java 使用jce, code: import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax ...
- HDU 5407(2015多校10)-CRB and Candies(组合数最小公倍数+乘法逆元)
题目地址:pid=5407">HDU 5407 题意:CRB有n颗不同的糖果,如今他要吃掉k颗(0<=k<=n),问k取0~n的方案数的最小公倍数是多少. 思路:首先做这道 ...
- popupwindow从底部弹出
参考了网上的一些代码,自己写了个类,上代码 /** * Created by Lee on 2016/2/26. */ public class CameraPopupWindow { private ...
- CF 316div2 E.Pig and Palindromes
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincide ...
- ubuntu中安装apache ab命令进行简单压力测试
1.安装ab命令 写道 apt-get install apache2-utils 2.ab命令参数说明. 写道 Usage: ab [options] [http[s]://]hostname[:p ...
- Windows 8 64bit Xilinx ISE(14.7) Fix License
http://www.youtube.com/watch?v=ttPbEcNjdo8 It can work successfully!
- jquery的text()
jQuery 文档操作 - text() 方法 jQuery 文档操作参考手册 实例 设置所有 <p> 元素的内容: $(".btn1").click(function ...
- SQL数据库 CRUD
1.删除表 drop table +表名 2.修改表 alter table+表名+ add(添加)+列名+ int(类型) alter table+表名+ drop(删除)+column(列) ...
- MVC4的缓存
目录(?)[+] MVC3缓存之一:使用页面缓存 在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中, ...
- AD使用技巧
1. 效果显示 3 显示3D效果,2显示2D效果. 2. 阻焊塞孔 双击过孔显示属性 solder Mask Expansions Force complete tenting on top Fo ...