hdu4059 The Boss on Mars(容斥)
来源:互联网 发布:pageoffice java word 编辑:程序博客网 时间:2024/05/19 04:05
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.
245
82354
Case1: sum=1+3*3*3*3=82Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
看了样例,题意应该比较好理解
需补充的知识点
1)1^4+2^4+3^4+……n^4=(6n^5+15n^4+10n^3-n)/30
2)逆元的知识
3)求解质因数
为啥用容斥呢?
对于样例1,输入4
可知2为质因数,由于不互质较难求得,我们求与n互质的
那么2为质因数,2的任意倍数都会被除去,且这些倍数的四次方和为2(1^4+2^4+……+(n/2)^4)
若质因数包括3,3的倍数也会被除去
但是2,3有公共倍数,需要用容斥处理。
容斥有三种实现方式,队列数组,dfs,二进制模拟,
我喜欢用队列数组
#include <iostream>using namespace std;typedef long long ll;const ll maxn=1e6;const ll mod=1000000007;ll prime[maxn];ll fac[maxn];ll flag[maxn];ll fast_pow(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans;}ll sum_n(ll n){ll ans;ans=((6*fast_pow(n,5)%mod+15*fast_pow(n,4)%mod+10*fast_pow(n,3)%mod-n+mod)%mod)*fast_pow(30,mod-2)%mod;return ans;}ll n_4(ll n){return fast_pow(n,4);}void solve(ll n){ll temp=n;ll num=0;for(int i=2;i*i<=n;i++){if(temp%i==0){prime[num++]=i;while(temp%i==0){temp/=i;}}}if(temp!=1)prime[num++]=temp;ll cnt=0;fac[cnt]=1;flag[cnt++]=1;for(int i=0;i<num;i++){ll temp_cnt=cnt;for(int j=0;j<temp_cnt;j++){fac[cnt]=fac[j]*prime[i];flag[cnt++]=flag[j]*-1;}}ll ans=0;for(int i=0;i<cnt;i++){ans=(ans+((flag[i]*n_4(fac[i]))%mod)*sum_n(n/fac[i])%mod+mod)%mod;}cout<<ans<<endl;}int main(){ll t;cin>>t;while(t--){ll n;cin>>n;solve(n);}return 0;}
阅读全文
0 0
- hdu4059 The Boss on Mars(容斥)
- hdu4059 The Boss on Mars(差分+容斥原理)
- hdu4059 The Boss on Mars
- hdu4059---The Boss on Mars(容斥原理+前n项的4次方和)
- HDU4059 The Boss on Mars【容斥原理】【乘法逆元】【高次求和】
- The Boss on Mars(容斥原理)
- HDU 4059 The Boss on Mars(容斥原理)
- hdu 4059 The Boss on Mars(容斥原理)
- ZOJ 3547 The Boss on Mars(容斥)
- ZOJ 3547 - The Boss on Mars(容斥)
- ZOJ 3547 The Boss on Mars(容斥)
- HDU 4059 The Boss on Mars(容斥原理)
- hdu 4059 The Boss on Mars(容斥原理)
- (hdu4059)The Boss on Mars(费马小定理+快速幂)
- ZOJ 3547 《The Boss on Mars》(容斥定理)
- zoj 3547 - The Boss on Mars(容斥原理)
- HDU 4059 The Boss on Mars【容斥原理】
- hdu 4059 The Boss on Mars 容斥原理
- 数据库Postgresql在使用Hibernate自动新增字段后插入不生效
- Ajax工作原理
- 移动端rem怎么用?
- Win10搭建Java开发环境
- 将内容布局延伸到状态栏中
- hdu4059 The Boss on Mars(容斥)
- vue axios 跨域问题
- 基于vue2.0.js的多级联动选择器
- layui 弹出的封装函数
- Android根据mapping.txt还原混淆的代码
- 华为服务器电源故障|开不了机故障灯处理
- ubuntu 分区设置
- jxl.jar入门
- 时间转换成各种样式的string