hdu4059 容斥定理+逆元(longlong计算分开写防溢出)

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #define MOD 1000000007
 5 long long  x,tmp,s2,n;
 6 long long  a[105],vis[10005],prime[10005];
 7 void exgcd(long long  a,int b,long long & d,long long & xx,long long& yy)
 8 {
 9     if (b==0) {d=a; xx=1; yy=0; }
10     else {exgcd(b,a%b,d,yy,xx); yy-=xx*(a/b);  }
11 }
12 long long ss(long long y)
13 {
14     long long t1=y*(y+1)%MOD;
15     long long t2=(2*y+1)*x%MOD;
16     long long t3=(((y*y)%MOD)*3+3*y-1)%MOD;
17     long long t4=(t1*t2)%MOD;
18     return (t3*t4)%MOD;
19 }
20 void dfs(long long now,long long flag,long long bei)
21 {
22     if (bei>n) return;
23     long long tt=n/bei;
24     long long temp=ss(tt);
25     temp=((((temp*bei)%MOD*bei)%MOD*bei)%MOD*bei)%MOD;
26     if (flag) s2=(s2+temp)%MOD;
27     else s2=(s2+MOD-temp)%MOD;
28     for (int i=now+1;i<=tmp;i++)
29         dfs(i,1-flag,bei*a[i]);
30 }
31 int main()
32 {
33     long long  cnt=0,i,j,y,n1,s1,d;
34     int T;
35     memset(vis,0,sizeof(vis));
36     for (i=2;i<=10000;i++)
37         if (vis[i]==0)
38         {
39             cnt++;
40             prime[cnt]=i;
41             for (j=2;i*j<=10000;j++) vis[i*j]=1;
42         }
43     exgcd(30,MOD,d,x,y);
44     x=(x+MOD)%MOD;
45     scanf("%d",&T);
46     while (T--)
47     {
48         scanf("%I64d",&n);
49         tmp=0; n1=n;
50         for (i=1;i<=cnt&&prime[i]<=n1;i++)
51         {
52             if (n1%prime[i]==0) {tmp++; a[tmp]=prime[i]; }
53             while (n1%prime[i]==0) n1/=prime[i];
54         }
55         if (n1!=1) {tmp++; a[tmp]=n1; }
56         s1=ss(n);
57         s2=0;
58         for (i=1;i<=tmp;i++)
59             dfs(i,1,a[i]);
60         printf("%I64d\n",(s1+MOD-s2)%MOD);
61     }
62 }

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

posted on 2014-10-28 14:34  xiao_xin  阅读(148)  评论(0编辑  收藏  举报

导航