标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2327 Accepted Submission(s):
718
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;
}
标签:
原文地址:http://www.cnblogs.com/xiaochaoqun/p/4898689.html