HDU 4059 The Boss on Mars (容斥+快速幂+分解质因数)
来源:互联网 发布:yum 安装snmp 编辑:程序博客网 时间:2024/05/16 07:24
The Boss on Mars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2304 Accepted Submission(s): 701
Problem Description
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss.
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.
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.
The first line contains an integer T indicating the number of test cases. (1 ≤ T ≤ 1000) Each test case, there is only one integer n, indicating the number of employees in ACM. (1 ≤ n ≤ 10^8)
For each test case, output an integer indicating the money the boss can save. Because the answer is so large, please module the answer with 1,000,000,007.
245
82354HintCase1: sum=1+3*3*3*3=82Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
ZHANG, Chao
2011 Asia Dalian Regional Contest
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4059
题目大意:求1-n中所有与n互质的数的四次方的和
题目分析:开始准备莫比乌斯反演,一看1e8,显然不行,只能分解质因数用DFS容斥了,先求出n^4的和:k*(k+1)*(2*k+1)*(3*k^2+3*k-1)/30,推导过程不写了,然后减去与n不互质的即可
#include <cstdio>#include <cstring>#define ll long longint const MOD = 1e9 + 7;int fac[10000], cnt;ll n, ans;ll qpow(ll x, ll n){ ll res = 1; while(n) { if(n & 1) res = (res * x) % MOD; x = (x * x) % MOD; n >>= 1; } return res;}ll cal(ll x) //1 + 2^4 + 3^4 + .. + x^4{ ll t1 = (x * (x + 1) % MOD) * ((2 * x + 1) % MOD); ll t2 = ((3 * x * x + 3 * x - 1) % MOD) * qpow(30ll, MOD - 2); return ((t1 % MOD) * (t2 % MOD)) % MOD;}void DFS(int pos, ll pre, int sgin){ if(pos >= cnt || pre > n) return; ll t = 1, cur = pre * fac[pos]; for(int i = 0; i < 4; i++) t = (t * cur) % MOD; t = (t * cal(n / cur)) % MOD; //所有cur的倍数的四次方和 ans = ((ans % MOD) + (sgin * t) % MOD + MOD) % MOD; //容斥 if(n % cur == 0) DFS(pos + 1, cur, -sgin); DFS(pos + 1, pre, sgin);}int main(){ int T; scanf("%d", &T); while(T--) { memset(fac, 0, sizeof(fac)); cnt = 0; scanf("%I64d", &n); ll nn = n; for(int i = 2; i * i <= nn; i++) { if(nn % i == 0) { fac[cnt ++] = i; while(nn % i == 0) nn /= i; } } if(nn != 1) fac[cnt ++] = nn; ans = cal(n); DFS(0, 1, -1); printf("%I64d\n", ans); } }
0 0
- HDU 4059 The Boss on Mars (容斥+快速幂+分解质因数)
- HDU 4059 The Boss on Mars(质数分解+容斥)
- HDU 4059 The Boss on Mars(容斥原理)
- hdu 4059 The Boss on Mars(容斥原理)
- HDU 4059 The Boss on Mars【容斥原理】
- hdu 4059 The Boss on Mars 容斥原理
- HDU 4059 The Boss on Mars-矩阵+容斥
- hdu 4059 The Boss on Mars(容斥)
- HDU 4059 The Boss on Mars ( 容斥原理)
- HDU 4059 The Boss on Mars(容斥原理)
- HDU 4059 The Boss on Mars(容斥原理)
- hdu 4059 The Boss on Mars(容斥原理)
- HDU 4059 The Boss on Mars 解题报告(4次方和+质因子分解+逆元+容斥原理)
- The Boss on Mars(容斥原理)
- 数论(容斥原理)hdu-4059-The Boss on Mars
- hdu 4059 The Boss on Mars 容斥原理 数列通项公式
- HDU 4059 The Boss on Mars 容斥原理+逆元
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
- .net micro framework Netduino无法休眠
- UIImageView
- Fundation构架
- AtionErrors和ActionMessages的区别
- iOS 手机短信验证码等待时间
- HDU 4059 The Boss on Mars (容斥+快速幂+分解质因数)
- Permutation总结
- 例题6-1 并行程序模拟 UVa210 双端队列
- 三目运算符 注意事项
- ios 数据持久化初级
- vpn修改端口号
- SQL学习记录
- Android常用控件之SoundPool
- linux下C/C++,多线程pthread