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

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
这道题目是求小于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(容斥原理)的更多相关文章

  1. HDU 4059 The Boss on Mars 容斥原理

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

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

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

  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. Js日常笔记之变量删除

    在Javascript是可以使用delete来手动删除变量,通过这样的方法让GC来回收内存,但在JS中并不是所有的对象都可以被删除的 JS中通过 var\function 声明因含有DontDelet ...

  2. mqtt选择

    1.名称 MQTT kafka 2.历史 IBM推出的一种针对移动终端设备的发布/预订协议. LinkedIn公司开发的分布式发布-订阅消息系统.后来,成为Apache项目的一部分. 3.原理 基于二 ...

  3. const和readonly关键字

    不知道大家对const和readonly这两个关键字的区别有什么了解,原来自己之前还真不清楚它们到底是怎么回事,那么如果你也不是很清楚的话,可以一起来探讨一下.在了解这两个关键字的时候我们先来了解一下 ...

  4. android 关于setWidth()和setHeight()没反应的问题

      在android开发过程中,对于控件的高度,宽度,虽然在xml中用android:layout_height="match_parent"设置了 高度(match_parent ...

  5. 弹出键盘windowsoftinputmode属性设置值

    windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifes ...

  6. spring中aop以xml配置方式

    1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...

  7. unity, 什么时候用静态类,什么时候用单例

    如果没有成员变量,或者成员变量都是常量,则用静态类. 如果有成员变量,则用单例.(以便让成员变量有初始化机会). //静态类 public class CmyFuncs{ public float m ...

  8. memcache命令行

    memcache运行状态可以方便的用stats命令显示. 首先用telnet 127.0.0.1 11211  [quit 退出]这样的命令连接上memcache,然后直接输入stats就可以得到当前 ...

  9. ArcGIS教程:将“替换为模型”工具用于多面体

    替换为模型工具出如今 3D 编辑器 工具条上的 3D 编辑器菜单中.而且仅仅适用于多面体要素.使用此命令可将所选的一个或多个要素的几何替换为磁盘中所保存的 3D 模型文件.受支持的 3D 模型类型包含 ...

  10. mongodb数据库shell

    mongoexport -d mofangdb -c log_user_access_index --type=csv -f _id,uid,page,date -o log_user_access_ ...