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): 1528 Accepted Submission(s): 452
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.
4
5
354
Case1: sum=1+3*3*3*3=82
Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
观察到与n互质的数的性质
比如12=2*2*3
那么与12不互质的数就有2,3,4,6,8,9,10,12
其实就是2的所有倍数,以及3的所有倍数
所以可以先求一个1到12的所有数的四次方和。这个有公式:
n*(n+1)*(6*n*n*n+9*n*n+n-1)/30(先开始就是抄错了。。)
注意这里30最好求一下逆元大概是2000多万
求的所有的四次方和之后当然要减去那些不互质的数的四次方
也就是说分别剪去了2和3的倍数的四次方,注意这里2和3的公倍数被多减去了,所以要加回来
所以容斥原理也要用
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const LL Mod= ;
const int M_P= ;
bool isprime[M_P+] ;
int prime[M_P] ,id;
vector<LL>vec;
void make_prime(){
memset(isprime,,sizeof(isprime)) ;
id= ;
for(int i=;i<=M_P;i++){
if(!isprime[i])
prime[++id]=i ;
for(int j=;j<=id&&i*prime[j]<=M_P;j++){
isprime[i*prime[j]]= ;
if(i%prime[j]==)
break ;
}
}
}
LL POW(LL x ,LL y){
LL ans= ;
for(;y;y>>=){
if(y&){
ans*=x ;
if(ans>=Mod)
ans%=Mod ;
}
x*=x ;
if(x>=Mod)
x%=Mod ;
}
return (ans+Mod)%Mod ;
}
LL J ;
LL Sum(LL N){
LL a=N%Mod ;
LL b=(N+)%Mod ;
LL c=*N%Mod ;
LL d=c*N%Mod ;
LL e=d*N%Mod ;
LL f=*N%Mod ;
LL g=f*N%Mod ;
LL h=(e+g+N-)%Mod ;
LL i=a*b%Mod ;
i=i*h%Mod ;
i=i*J%Mod ;
return (i+Mod)%Mod ;
}
LL gao_coprime_sum(LL N){
LL ans= ;
int n=vec.size() ;
for(int i=;i<(<<n);i++){
int now= ;
LL pri= ;
for(int j=;j<n;j++){
if(i&(<<j)){
now++ ;
pri*=vec[j] ;
}
}
if(now&){
ans=ans+POW(pri,)*Sum(N/pri) ;
if(ans>=Mod)
ans%=Mod ;
}
else{
ans=ans-POW(pri,)*Sum(N/pri) ;
if(ans>=Mod)
ans%=Mod ;
}
}
return ((Sum(N)-ans)%Mod+Mod)%Mod ;
}
int main(){
make_prime() ;
J=POW(,Mod-) ;
LL N ,T;
cin>>T ;
while(T--){
cin>>N ;
vec.clear() ;
LL M=N ;
for(int i=;i<=id&&prime[i]*prime[i]<=M;i++){
if(M%prime[i]==){
vec.push_back(prime[i]) ;
while(M%prime[i]==)
M/=prime[i] ;
}
if(M==)
break ;
}
if(M!=)
vec.push_back(M) ;
cout<<gao_coprime_sum(N)<<endl ;
}
return ;
}
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/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 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 ...
随机推荐
- 安装Android sdk 4.4(19)出现问题的解决方案
刚更新了Android sdk 19,但是出现以下两个问题,浪费我2个小时的时间,现在将我遇到的问题和解决方法总结如下: 问题1:打开eclipse点更新后,出现This Android SDK re ...
- Linux环境下vsftpd参数配置
很久之前就用过vsftpd,但总是忘了参数该如何配置,今天特地有搜索了一遍,把配置方法整理如下: (1)检查是否已安装vsftpd #rpm -qa | grep vsftpd vsftpd--.el ...
- android学习笔记23——菜单
菜单在桌面应用程序中使用非常广泛,由于手机屏幕的制约,菜单在手机应用中减少不少. android应用中的菜单默认是不可见的,只有当用户单击手机上“Menu”键时,系统才会显示该应用关联的采用项. an ...
- sql数据库带补全终端命令
mysql pip install mycli pgsql pip install pgcli 都是python脚本,记录备忘.
- golang的采集库
goquery https://github.com/PuerkitoBio/goquery 例子 aa.html <html> <body> <div id=" ...
- VBA标准模块与类模块
大家通过之前的介绍,已知道怎么将一个空模块插入VBA的工程中.从插入模块中可以看到,模块有有两种——标准模块与类模块.类模块是含有类定义的特殊模块,包括其属性和方法的定义.在后面会有介绍与说明. 随着 ...
- 求助:为什么我用360浏览器和UC浏览器打不开JAVA中的index.html文件? 一打开就显示浏览器首界页
如下图,在oracle官网下载了一个JAVA的API文档,双击index.html时打开是浏览器的首页,不知道为什么?请问怎样才能以chm文档格式显示?
- gcc和ld 中的参数 --whole-archive 和 --no-whole-archive
首先 --whole-archive 和 --no-whole-archive 是ld专有的命令行参数,gcc 并不认识,要通gcc传递到 ld,需要在他们前面加 -Wl,字串. --whole-ar ...
- flash文件制作笔记
在uboot串口台输入printenv 可以分区以及其他信息,如下 hisilicon # printenv bootdelay=1baudrate=115200ethaddr=00:00:23:34 ...
- Android框架
http://blog.163.com/vicent_zxb/blog/static/1858861312011488262665/ (一)Android系统框架详解 Android采用分层的架构,分 ...