BZOJ 3944 Sum
来源:互联网 发布:mac系统如何格式化u盘 编辑:程序博客网 时间:2024/05/17 08:21
Description
Input
一共T+1行
第1行为数据组数T(T<=10)
第2~T+1行每行一个非负整数N,代表一组询问
Output
一共T行,每行两个用空格分隔的数ans1,ans2
Sample Input
6
1
2
8
13
30
2333
1
2
8
13
30
2333
Sample Output
1 1
2 0
22 -2
58 -3
278 -3
1655470 2
2 0
22 -2
58 -3
278 -3
1655470 2
HINT
Source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~杜教筛~
PoPoQQQ大爷的题解传送门:http://blog.csdn.net/popoqqq/article/details/45023331,有详细的推导~
另:如果发现我的代码在BZOJ上T了,只要多交几次就可以了,具体次数看脸~神奇的评测机~
#include<cstdio>#include<map>using namespace std;#define ll long longint t,n,q[1000000];ll phi[5000000],u[5000000];bool b[5000000];map<int,ll> fu,fphi;ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void init(){phi[1]=u[1]=1;for(ll i=2;i<5000000;i++){if(!b[i]) q[++q[0]]=i,phi[i]=i-1,u[i]=-1;for(ll j=1;q[j]*i<5000000 && j<=q[0];j++){b[i*q[j]]=1;if(!(i%q[j])){phi[i*q[j]]=phi[i]*q[j];u[i*q[j]]=0;break;}phi[i*q[j]]=phi[i]*(q[j]-1);u[i*q[j]]=-u[i];}}for(ll i=2;i<5000000;i++) phi[i]+=phi[i-1],u[i]+=u[i-1];}ll cal_phi(ll n){if(n<5000000) return phi[n];map<int,ll>::iterator it=fphi.find(n);if(it!=fphi.end()) return it->second;ll las,re=(ll)n*(n+1)>>1;for(ll i=2;i<=n;i=las+1){las=n/(n/i);re-=(las-i+1)*cal_phi(n/i);}return fphi[n]=re;}ll cal_u(ll n){if(n<5000000) return u[n];map<int,ll>::iterator it=fu.find(n);if(it!=fu.end()) return it->second;ll las,re=1;for(ll i=2;i<=n;i=las+1){las=n/(n/i);re-=(las-i+1)*cal_u(n/i);}return fu[n]=re;}int main(){init();t=read();while(t--){n=read();printf("%lld %lld\n",cal_phi(n),cal_u(n));}return 0;}
阅读全文
1 0
- BZOJ 3944 Sum 数论
- bzoj 3944: Sum 杜教筛
- bzoj 3944: Sum 杜教筛
- 【BZOJ 3944】sum 杜教筛
- bzoj 3944 sum 杜教筛
- BZOJ 3944: Sum
- bzoj 3944 Sum 杜教筛
- BZOJ 3944 Sum
- bzoj 3944 Sum
- [BZOJ 3944]Sum:杜教筛
- [BZOJ]3944: Sum
- bzoj 3944: Sum
- bzoj 3944 Sum
- BZOJ 3944: Sum 杜教筛
- bzoj 3944 Sum 杜教筛
- [BZOJ]3944 Sum 杜教筛
- [杜教筛模板] BZOJ 3944 Sum
- bzoj 3944: Sum (杜教筛)
- Redis持久化:aof和rdb
- 中国剩余定理题——Biorhythms
- Android 之 控件的背景样式总结
- 正则案例
- STM32学习之普通定时器是否能定时1us?
- BZOJ 3944 Sum
- qt学习笔记:Qt标准对话框之QInputDialog
- spring中JdbcTemplate
- 一个maven项目pom配置
- git别名
- hnust
- WebSocket 是什么原理?为什么可以实现持久连接?
- LeetCode 202. Happy Number (迭代,各位平方和)
- BZOJ 4320: ShangHai2006 Homework