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 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

http://acm.hdu.edu.cn/showproblem.php?pid=4059

///这题主要是公式问题,然后稍微对容斥原理计算个数,改为mult的特殊利用即可;

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

///1~n这n个数的4次方和:n*(n+1)*(6*n*n*n+9*n*n+n-1)/30; 这里百度的公式;

typedef long long ll;

const ll N = 1e6+5;

const ll mod = 1e9+7;

ll n, ans;

ll get4mult(ll mult)

{

    return mult*mult%mod*mult%mod*mult%mod;

}

ll extgcd(ll a,ll b,ll &x,ll &y)

{

    if(b==0){

        x = 1, y = 0; return a;

    }

    ll d = extgcd(b,a%b,x,y);

    ll t = x;

    x = y;

    y = t-a/b*y;

    return d;

}

ll inverse(ll t)

{

    ll x, y;

    ll d = extgcd(t,mod,x,y);

    return (x%mod+mod)%mod;

}

ll calu(ll n)

{

    return n*(n+1)%mod*(6*n*n%mod*n%mod+9*n*n%mod+n-1)%mod*inverse(30)%mod;

}

ll rongchi(ll n)

{

    ll m = n;

    vector<ll> v;

    for(ll i = 2; i*i <= m; i++){

        if(m%i==0){

            v.push_back(i);

            while(m%i==0) m/=i;

        }

    }

    if(m>1) v.push_back(m);

    ll len = v.size();

    for(ll i = 1; i < (1<<len); i++){

        ll mult = 1, ones = 0;

        for(ll j = 0; j < len; j++){

            if(i&(1<<j)){

                ones++;

                mult = mult*v[j];

            }

        }

        if(ones%2) {/// 变成了 -  因为假设是原来的容斥,那么cnt指的是不互质的个数,是要被剪掉的;

                ///而本题:也是要减掉,但是不是计算个数,而是对每一个过程有作用,减去:减变为了加,加变为了减;

            ans = ans-get4mult(mult)*calu(n/mult);

            ans = (ans%mod+mod)%mod;

        }else/// 变成了 +

        {

            ans = ans+get4mult(mult)*calu(n/mult);

            ans = (ans%mod+mod)%mod;

        }

    }

    v.clear();

    return ans;

}

int main()

{

    ll T;

    cin>>T;

    while(T--)

    {

        cin>>n;

        ans = calu(n);

        printf("%lld\n",rongchi(n));

    }

    return 0;

}

时间: 2024-08-10 20:54:04

The Boss on Mars的相关文章

数论 + 容斥 - 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

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(数论)

题目大意:给你一个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

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

『ZOJ 3547』The Boss on Mars (容斥原理)

传送门戳这里qwq 题目描述 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 nemployees in ACM, and it’s time for them

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 定义S = 1^4 + 2^4 + 3^4+.....+n^4,现在减去与n互质的数的4次方,问共减少了多少. 容斥原理,可以先把与n不互质的数的4次方求出来.那就先对n进行质因子分解,对质因子的组合运用容斥原理,质因子个数为奇数就加,偶数就减.其实与求[1,n]内与n互质的数的个数类似,该题重点是计算,防止乘法溢出. 对于求解1^4 + 2^4 + 3^4+.....+n^4,可以先类比1^2+2^2

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次方.问共降低了多少. 容斥原理.能够先把与n不互质的数的4次方求出来.那就先对n进行质因子分解,对质因子的组合运用容斥原理.质因子个数为奇数就加,偶数就减.事实上与求[1,n]内与n互质的数的个数类似,该题重点是计算,防止乘法溢出. 对于求解1^4 + 2^4 + 3^4+.....+n^4,能够先类比1^2+2^

HDU 4059:The Boss on Mars(数学公式+容斥原理)

http://acm.hdu.edu.cn/showproblem.php?pid=4059 题意:给出一个n,求1~n里面与n互质的数的四次方的和是多少. 思路:不知道1~n的每个数的四次方的求和公式.看的是这篇:http://blog.csdn.net/acm_cxlove/article/details/7434864. 求和公式:(1^4+2^4+……+n^4)=(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30; 然后先求出1~n的每个数的四次方的求和,然后再减去n的因