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

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+10n3?n30

由于要取模,所以要用下逆元

/*************************************************************************
    > File Name: hdu4059.cpp
    > Author: ALex
    > Mail: [email protected]
    > 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;
}
时间: 2024-10-17 14:53:04

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

HDU4059 The Boss on Mars【容斥原理】【乘法逆元】【高次求和】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题目大意: 给一个整数N,求1~N中与N互质的数的4次方的和. 解题思路: 题目简单,过程有点复杂.理清思路就简单了. 利用公式1^4 + 2^4 + - + n^4 = n*(n+1)*(2*n+1)*(3*n^2+3*n-1)/30,可以求出 1^4 + 2^4 + - + n^4,除以30可以先求出30模M的逆元,然后将上式中除以30改为 乘以30的逆元. 再来求与n不互质的数的4次方

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)为b的逆元 //由费马小定理a^(p-1) = 1(modp) a , p 互质可得 //30的逆元为30^(mod-2) //由容斥原理很容易得到与n不互质的数之和为 //对于所有的n的素数因子 //一个素数因子的所有数的四次方之和-有两个素数因子的所有数的四次方之和+有三个.... //然后用

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

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题意: 给定一个数n求小于n的与n互斥的数的四次方的和. 分析: 我们可以求出从1~n的所有数的四次方的和sum1,然后容斥求出1~n所有与n不互斥的数的四次方的和sum2: ans =sum1 - sum2; 设f(n)表示从1~n的所有数的四次方的和 f(n)=1/30*n*(n+1)(2n+1)(3n^2+3n-1); 推倒如下: (n+1)^5-n^5=5n^4+10n^3+10n^

The Boss on Mars

The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2327    Accepted Submission(s): 718 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), and

hdu 4059 The Boss on Mars

The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1934    Accepted Submission(s): 580 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), an

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

The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若自己手动推公式的话,还是需要一定的数学基础. 总的思路:先求出sum1=(1^4)+(2^4)+...(n^4),再求出sum2=(1~n中与n不互质的数的四次方的和),answer=sum1-sum2. 如何求sum1呢? 有两种方法: 1.数列差分.由于A={Sn}={a1^4+a2^4+...an^4}

HDU 4059 The Boss on Mars(数论)

题目大意:给你一个n(10^8)以内,让你求出1-n中与n互质的数x^4的和. 解题思路:先把n进行分解质因数,然后容斥求出所有与n不互质的数x^4的和,然后做减法用总的减去不互质的就是互质的. 注意:1^4+2^4+--+n^4 = n(n+1)(2n+1)(3n^2+3n-1)/30. The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot

HDU - 4059: The Boss on Mars (容斥 拉格朗日 优化)

pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol:  因为N比较小,我们可以求出素因子,然后容斥.  主要问题就是求1到P的4次幂和.  我们知道K次幂和是一个K+1次多项式. 这里有公式Pre=P*(P+1)*(2P+1)*(3P^2+3P-1)/30; 在不知道公式的情况下,我们可以用拉格朗日差值法求. 1,下面给出DFS容斥的代码 . #include<bits/stdc++.h> #define ll long

求f(k)=k^k(k=1...n)的前n项和

求f(k)=k^k(k=1...n)的前n项和. 程序实现: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> long long My_Mul_Sum(int *n)//封装了一个求k^k的前n项和的函数 { int k = 1; long long sum = 0;//定义为long long是为了防止数据较大,容易溢出 for (k = 1; k <= n; k++) { int count = 0, mul = 1;//count