HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2494 Accepted Submission(s): 775
Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number
is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.
Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.
2 4 5
82 354 这道题目是求小于n和n互质的数的四次方的和 利用容斥原理,可以用总和减去不和n互质的和 也可以直接求互质的数的和 这里还涉及到了sum{1^4,2^4,3^4....n^4}求和公式 n*(n+1)*(2*n+1)*((3*n*n+3*n-1)/30 求和公式里有/ 那么在用同余定理的时候就要用逆元 b/c(mod m)=b(mod m)* c^(m-2)(mod m)证明略
#include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <stdio.h> #include <stdlib.h> using namespace std; typedef long long int LL; #define MAX 1000000 LL prime[MAX+5]; LL sprime[MAX+5]; bool check[MAX+5]; const LL mod=1e9+7; LL INF; LL cnt; LL quick(LL x,LL a) { LL sum=1; for(x;x>0;x>>=1) { if(x&1) sum=(sum*a)%mod; a=(a*a)%mod; } return sum; } LL sum1(LL n) { INF=quick(mod-2,30); return n*(n+1)%mod*(2*n+1)%mod*((3*n*n%mod+3*n-1)%mod)%mod*INF%mod; } LL sum2(LL n) { return (((n*n)%mod*n)%mod*n)%mod; } void eular() { memset(check,false,sizeof(check)); int tot=0; for(LL i=2;i<MAX+5;i++) { if(!check[i]) prime[tot++]=i; for(int j=0;j<tot;j++) { if(i*prime[j]>MAX+5) break; check[i*prime[j]]=true; if(i%prime[j]==0) break; } } } void Divide(LL n) { cnt=0; LL t=(LL)sqrt(n*1.0); for(LL i=0;prime[i]<=t;i++) { if(n%prime[i]==0) { sprime[cnt++]=prime[i]; while(n%prime[i]==0) n/=prime[i]; } } if(n>1) sprime[cnt++]=n; } int main() { eular(); int t; scanf("%d",&t); while(t--) { LL n; scanf("%lld",&n); Divide(n); LL ans=0; LL res=sum1(n); for(int i=1;i<((LL)(1<<(cnt)));i++) { int num=0; LL tmp=1; for(int j=0;j<cnt;j++) { if(i&(1<<j)) { num++; tmp*=sprime[j]; } } if(num&1) ans=(ans+(sum1(n/tmp)*sum2(tmp))%mod)%mod; else ans=((ans-(sum1(n/tmp)*sum2(tmp))%mod)%mod+mod)%mod; } printf("%lld\n",(res-ans+mod)%mod); } return 0;}
HDU 4059 The Boss on Mars(容斥原理)的更多相关文章
- HDU 4059 The Boss on Mars 容斥原理
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 数论 + 容斥 - HDU 4059 The Boss on Mars
The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...
- hdu 4059 The Boss on Mars(纳入和排除)
http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多 ...
- hdu 4059 The Boss on Mars 容斥
题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...
- hdu4059 The Boss on Mars 容斥原理
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger bo ...
- hdu 4059 数论+高次方求和+容斥原理
http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...
- hdu4059The Boss on Mars 容斥原理
//求1到n之间与n互质的数的四次方的和 //segma(n^4) = (6n^5+15n^4+10n^3-n)/30 //对于(a/b)%mod能够转化为(a*inv(b))%mod //inv(b ...
随机推荐
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 几种TCP连接中出现RST的情况(转载)
TCP RST 网络 linux 目录[-] 1 端口未打开 2 请求超时 3 提前关闭 4 在一个已关闭的socket上收到数据 总结 参考文献: 应该没有人会质疑,现在是一个网络时代了.应该不少程 ...
- C# LDAP
Lightweight Directory Access Protocol OpenLDAP
- Request介绍及演示样例 PART1
Request在ServletAPI的规范连接地址http://blog.csdn.net/zghwaicsdn/article/details/51035146 HTTP简介 URL是浏览器寻找信息 ...
- Atitit.用户权限服务 登录退出功能
Atitit.用户权限服务 登录退出功能 参数说明 /com.attilax/user/loginOut.jsp?url="+url Utype=mer 作者:: ★(attilax)&g ...
- 深入浅出FPGA-2-让source insight 支持verilog HDL
引言 工欲善其事,必先利其器.最近用verilog在FPGA上做一个简易的计算器,整个工程由6个模块组成,之前写的代码都是很小的,模块也很少,一般就一两个,没感觉出别扭.但是模块多了就发现,模块之间的 ...
- DirectShow中写push模式的source filter流程 + 源码(内附具体凝视)
尽管网上已有非常多关于DirectShow写source filter的资料.只是非常多刚開始学的朋友总说讲的不是非常清楚(可能当中作者省略了很多他觉得简 单的过程).读者总希望看到象第一步怎么做,第 ...
- C++语言基础(24)-四种类型转换运算符(static_cast、dynamic_cast、const_cast和reinterpret_cast)
一.static_cast static_cast 只能用于良性转换,这样的转换风险较低,一般不会发生什么意外,如: #include <iostream> #include <cs ...
- 李洪强和你一起学习前端之(8)CSS复习
今天是2017年3月24日周五 每一天都是余生当中最好的一天,珍惜当下. CSS基础复习 1 复习 1.1Css第一天 css层叠样式表 基础选择器 标签选择器 p{属性: 值;} 类选择器 .自定义 ...
- 分时段显示不同的提示的网页JS特效代码
脚本说明: 把如下代码加入body区域中 <SCRIPT> today=new Date(); var day; var date; var hello; var wel; hour=ne ...