hdu4059---The Boss on Mars(容斥原理+前n项的4次方和)

来源:互联网 发布:氟硝西泮片淘宝店 编辑:程序博客网 时间:2024/05/01 17:40

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
2011 Asia Dalian Regional Contest

Recommend

首先考虑n的素因子,然后统计可以用容斥
14+24+34+...+n4可以尝试推公式
公式是:
S(n)= 6n5+15n4+10n3n30
由于要取模,所以要用下逆元

/*************************************************************************    > File Name: hdu4059.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年05月28日 星期四 16时49分35秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;vector <int> mst;const int mod = 1000000007;LL calc(LL n) {    LL ans = 0;    LL A = 6LL;    for (int i = 1; i <= 5; ++i) {        A *= n;        A %= mod;    }    ans += A;    A = 15LL;    for (int i = 1; i <= 4; ++i) {        A *= n;        A %= mod;    }    ans += A;    A = 10LL;    for (int i = 1; i <= 3; ++i) {        A *= n;        A %= mod;    }    ans += A;    ans -= n;    ans = (ans % mod + mod) % mod;    ans *= (233333335LL % mod);    ans %= mod;    return ans;}int main() {    int t;    scanf("%d", &t);    while (t--) {        LL n, m;        scanf("%lld", &n);        m = n;        mst.clear();        for (int i = 2; i * i <= n; ++i) {            if (n % i == 0) {                mst.push_back(i);                while (n % i == 0) {                    n /= i;                }            }        }        if (n > 1) {            mst.push_back(n);        }        LL ans = calc(m);        LL C = 0;        int size = mst.size();        for (int i = 1; i < (1 << size); ++i) {            int bits = 0;            int num = 1;            for (int j = 0; j < size; ++j) {                if (i & (1 << j)) {                    ++bits;                    num *= mst[j];                }            }            int k = m / num;            LL tmp = num;            tmp *= num;            tmp %= mod;            tmp *= num;            tmp %= mod;            tmp *= num;            tmp %= mod;            tmp *= calc((LL)k);            tmp %= mod;            if (bits & 1) {                C += tmp;            }            else {                C -= tmp;            }            C = (C % mod + mod) % mod;        }        ans -= C;        ans = (ans % mod + mod) % mod;        printf("%lld\n", ans);    }    return 0;}
1 0