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

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.

 
Input
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)
 
Output
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.
 
Sample Input
2
4
5
 
Sample Output
82
354

Hint

Case1: sum=1+3*3*3*3=82
Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354

 
Author
ZHANG, Chao
 
Source
 
     我喜欢非递归写法,即直接用二进制数去模拟。

观察到与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 容斥原理的更多相关文章

  1. HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)

    传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 4059 The Boss on Mars(容斥原理)

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hdu 4059 The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 数论 + 容斥 - HDU 4059 The Boss on Mars

    The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...

  5. 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次方.问共降低了多 ...

  6. hdu 4059 The Boss on Mars 容斥

    题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...

  7. 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 ...

  8. hdu 4059 数论+高次方求和+容斥原理

    http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...

  9. 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 ...

随机推荐

  1. SVN-钩子祥解

    钩子脚本的具体写法就是操作系统中shell脚本程序的写法,请根据自己SVN所在的操作系统和shell程序进行相应的写作 所谓钩子就是与一些版本库事件触发的程序,例如新修订版本的创建,或是未版本化属性的 ...

  2. WinForm窗体拖动代码

    本文转载自:http://www.cnblogs.com/ap0606122/archive/2012/10/23/2734964.html using System; using System.Co ...

  3. C语言每日一题之No.5

    总在想,但凡编程基础正常点,都不至于惨败到这个地步.也像大多数人毕业出来,新鲜的第一份工作,如果做得好还可以略有成就感,做得一般还有提升的空间,但至少不至于像我这样基本没基础的被鄙视得一塌糊涂,被外界 ...

  4. 在C#中子线程如何操作主线程中窗体上控件

    在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...

  5. file_get_content和curl的性能比较

    今天在获取微信一张二维码图片时发现使用php中的file_get_content方式和curl方式竟然相差了50倍左右,直接晕倒!!!

  6. Hibernae 的延迟加载

    http://blog.csdn.net/xc635960736/article/details/7049863 Hibernae 的延迟加载   Hibernae 的延迟加载是一个非常常用的技术,实 ...

  7. 编译器错误消息: CS0234: 命名空间“Purple”中不存在类型或命名空间名称“Model”(是否缺少程序集引用?)

    编译错误 “/storeimg”应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS023 ...

  8. JAVA 构造方法之间的调用

    this:看上去,用来区分局部变量和成员变量的情况this:就是代表本类对象,this代表它所在方法所属对象的引用构造方法之间的调用只能通过this语句来完成构造方法之间进行调用时this语句只能出现 ...

  9. LInux SSH远程文件/目录传输命令scp(转载)

    From:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS是经常会在不同VPS间相互备份数据或转移数据.一般情况下VPS都已安装了Ngnix或者 ...

  10. 如何用js来判断浏览器类型(ie,firefox)等等

    现在网络上的浏览器,操作系统就象中国的方言一样,那个叫多啊!这给我们这些开发人员 带来了巨大的痛苦!虽然可能大家的喜好不同!用的系统也不同!有人喜欢用ie,有人喜欢用 firefox,还有人喜欢用腾讯 ...