General Formula for the principle of hdu 4059 refresh

Source: Internet
Author: User

In fact, the difficulty of this question lies in the details of Large Numbers crossing the border, taking the remainder, and so on.

First, find the generic formula

n(2n+1)(n+1)(3n² +3n-1)/30
There are two ways to evaluate (a/B) % mod
1. Original formula = a % (B * n)/B
2. Original formula = a * B ^ (phi (mod)-1) % mod; (in which B interacts with mod)
In this question, because mod is too large, the first method will cause an error, so the second method is used.
For example, four times of 2, four times of 4, and four times of 6 are added in the Process of rejection .... It can be extracted four times of 2, and the other item is changed to four vertices.
View Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const __int64 mod = 1000000007;
__int64 n;
__int64 power(__int64 a,__int64 b,__int64 c)
{
__int64 ret=1;
while(b>0)
{
if(b&1)
ret=ret*a%c;
a=a*a%c;
b>>=1;
}
return ret;
}
__int64 calc(__int64 n)
{
__int64 a=n,b=(2*n+1),c=(n+1),d=(3*n*n+3*n-1)%mod;
__int64 sum=1;
sum*=a;sum%=mod;
sum*=b;sum%=mod;
sum*=c;sum%=mod;
sum*=d;sum%=mod;
__int64 t=power(30,mod-2,mod);
return sum*t%mod;
}
__int64 sici(__int64 n)
{
__int64 sum=1;
for(int i=1;i<=4;i++)
{
sum*=n;
if(sum>=mod) sum%=mod;
}
return sum;
}
__int64 solve(__int64 r,__int64 n)
{
vector<__int64> p;
__int64 i;
for(i=2;i*i<=r;i++)
{
if(r%i==0)
{
p.push_back(i);
while(r%i==0) r/=i;
}
}
if(r>1) p.push_back(r);
__int64 sum=0;
for(__int64 num=1;num<(1<<p.size());num++)
{
__int64 mult=1,ones=0;
for(i=0;i<p.size();i++)
{
if(num&(1<<i))
{
ones++;
mult*=p[i];
if(mult>=mod) mult%=mod;
}
}
if(ones%2) sum+=sici(mult)*calc(n/mult);
else sum-=sici(mult)*calc(n/mult);
sum%=mod;
}
return sum;
}
int main()
{
__int64 t,i,j;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d",&n);
printf("%I64d\n",(calc(n)-solve(n,n)%mod+mod)%mod);
}
return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.