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. NSArray、NSMutableArray和NSMutableDictionary的用法

    转自:http://www.cnblogs.com/wangpei/admin/EditPosts.aspx?opt=1 NSArray是静态的数组,就是它所指向的内容是不可改变的,它指向一段内存区域 ...

  2. 设计模式_Observable与Observer

    一.基本概念   java.util.Observable 被观察者类,需要继承这个类   java.util.Observer 观察者类,需要实现这个接口中的update()方法 二.举例 Door ...

  3. python学习之join()

    str.join(iterable) 该方法用来分隔字符串的. 例子 >>> b':'.join((b'leo',b'999')) b'leo:999' >>> ' ...

  4. pair + map 函数结合使用

    题目链接:codeforces 44A5birch yellowmaple redbirch yellowmaple yellowmaple green 4 3oak yellowoak yellow ...

  5. C/C++开发平时用的自定义debug函数

    一.无颜色版 一.自定义printf #include <stdio.h> #ifdef MYDEBUG #define DEBUG(arg...) {\ printf("[de ...

  6. jquery仿jquery mobile的select控件效果

    不说废话.直接上代码 //仿jQuery mobile Select控件 //使用方法box为容器id,_id指控件id,selectvalue为选中值,Value为当前值 function Sele ...

  7. 如何编写Makefile?

    //swap.c #include<stdio.h> int swap(int *x,int *y) {printf("a=%d b=%d\n",*x,*y); int ...

  8. 多线程中wait和notify的理解与使用

    1.对于wait()和notify()的理解 对于wait()和notify()的理解,还是要从jdk官方文档中开始,在Object类方法中有: void notify()  Wakes up a s ...

  9. 【tyvj】P2065 「Poetize10」封印一击(贪心+线段树/差分)

    http://new.tyvj.cn/p/2065 我就不说我很sb的用线段树来维护值...... 本机自测的时候想了老半天没想出怎么维护点在所有区间被多少区间包含的方法.最后一小时才想出来线段树(果 ...

  10. 首次接触XAMPP,端口被占用困恼

    本人运气比较好,首次安装XAMPP就碰到了各种问题啊!并且已经解决,以下是我问题的出处并且解决. 问题描述: apache无法打开,并且连带的出现了mySql无法打开.(即80端口冲突问题) 解决办法 ...