首页 > 代码库 > HDU 4059 The Boss on Mars(数论)

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)
Total Submission(s): 2035    Accepted Submission(s): 611


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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL long long
//#define LL long long
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007


const int maxn = 110;

using namespace std;
///bool f[maxn];

int num[maxn];
int ans;
void Find(LL n)
{
    ans = 0;
    for(int i = 2; i*i <= n; i++)
    {
        if(n%i==0)
        {
            num[ans++] = i;
            while(n%i == 0) n /= i;
        }
    }
    if(n > 1) num[ans++] = n;
}

LL fastmod(LL a, LL k)
{
    LL b = 1LL;
    while(k)
    {
        if(k&1) b = a*b%mod;
        a = (a%mod)*(a%mod)%mod;
        k /= 2;
    }
    return b;
}

LL Pow(LL n, int k)
{
    LL ff = 1LL;
    for(int i = 0; i < k; i++) 
    {
        ff *= n;
        if(ff > mod) ff %= mod;
    }
    return ff%mod;
}

LL Get(LL n)
{
    LL sum = n*(n+1)%mod;
    sum %= mod;
    sum *= (2*n+1)%mod;
    sum %= mod;
    sum *= ((3*(n*n%mod))%mod+((3*n)%mod)-1)%mod;
    sum %= mod;
    LL sp = fastmod(30, mod-2);
    sum *= sp;
    sum %= mod;
    return sum;
}

int main()
{
    int T;
    cin >>T;
    while(T--)
    {
        LL n;
        scanf("%I64d",&n);
        Find(n);
        LL sum = 0;
        LL xsum;
        LL xans = Get(n);
        for(int i = 1; i < (1<<ans); i++)
        {
            LL sx = 1;
            int s = 0;
            for(int j = 0; j < ans; j++)
            {
                if(i&(1<<j))
                {
                    sx *= num[j];
                    s ++;
                }
            }
            if(s%2)
            {
                xsum = Pow(sx, 4);
                xsum %= mod;
                sum += (xsum*Get(n/sx))%mod;
                sum %= mod;
            }
            else
            {
                xsum = Pow(sx, 4);
                xsum %= mod;
                sum -= (xsum*Get(n/sx))%mod;
                sum %= mod;
                while(sum < 0) sum += mod;
            }
        }
        if(n == 1)
        {
            cout<<0<<endl;
            continue;
        }
        xans -= sum;
        while(xans < 0) xans += mod;
        printf("%I64d\n",xans%mod);
    }
}


HDU 4059 The Boss on Mars(数论)