2012-11-04 17:32:36Morris

[ACM-ICPC][排容、逆元] 5701 - 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 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 k4 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$ le$T$ le$1000) Each test case, there is only one integer n, indicating the number of employees in ACM. ( 1$ le$n$ le$108)

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.


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

Sample Input 

 
2
4
5

Sample Output 

 
82
354

導不出來公式解,就玩排容原理,順便背了 sigma(n^4) 的公式
n*(n+1)*(2*n+1)*(3*n*n+3*n-1)/30,因為 mod 一個質數,因此除 30 換成 乘30的逆元。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cstdlib>
#include <math.h>
#include<map>
#define mem(x) memset(x,0,sizeof(x))
using namespace std;

typedef long long ll;

#define MOD 1000000007LL
int p[30], pt = 0;
int n, on;
long long inv(long long x, long long y) {
if(y == 0) return 1;
if(y&1) {
return x*inv(x*x%MOD, y/2)%MOD;
} else {
return inv(x*x%MOD, y/2);
}
}
long long ans, inv30;
long long n44(long long n) {
return n*(n+1)%MOD*(n*2+1)%MOD*(n*n*3%MOD+n*3-1)%MOD*inv30%MOD;
}
void dfs(int idx, int cnt, int num) {
if(idx == pt) {
if(cnt == 0) return;
long long base = (long long)num*num%MOD*num%MOD*num%MOD;
long long i = on/num;
if(cnt&1) {
ans -= base*n44(i);
} else {
ans += base*n44(i);
}
ans %= MOD;
return;
}
dfs(idx+1, cnt+1, num*p[idx]);
dfs(idx+1, cnt, num);
}
int main(){
int t, i;
inv30 = inv(30, MOD-2);
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
int sq = (int)sqrt(n);
on = n;
pt = 0;
for(i = 2; i <= sq; i++) {
if(n%i == 0) {
p[pt++] = i;
while(n%i == 0)
n /=i ;
}
}
if(n != 1) p[pt++] = n;
ans = n44(on)%MOD;
dfs(0, 0, 1);
printf("%lld", (ans%MOD+MOD)%MOD);
puts("");
}
return 0;
}