标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 809 Accepted Submission(s): 283
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<map> 6 #include<queue> 7 #include<stack> 8 #include<set> 9 #define ll __int64 10 #define mod 100000007 11 using namespace std; 12 ll dp[1005][1005]; 13 int a[1005]; 14 int maxn; 15 int t,n; 16 ll ans=0; 17 ll gcd(ll aa,ll bb)// 求解gcd 18 { 19 ll exm; 20 if(aa<bb) 21 { 22 exm=aa; 23 aa=bb; 24 bb=exm; 25 } 26 if(bb==0) 27 return aa; 28 gcd(bb,aa%bb); 29 } 30 int main() 31 { 32 while(scanf("%d",&t)!=EOF) 33 { 34 for(int i=1; i<=t; i++) 35 { 36 ans=0; 37 memset(dp,0,sizeof(dp)); 38 memset(a,0,sizeof(a)); 39 scanf("%d",&n); 40 maxn=-1; 41 for(int j=1; j<=n; j++) 42 { 43 scanf("%d",&a[j]); 44 if(a[j]>maxn) 45 maxn=a[j]; 46 dp[j][a[j]]=1;//初始化 只取第j个a[j] 47 } 48 for(int j=1; j<=n; j++) 49 { 50 int v=a[j+1]; 51 for(int k=1; k<=maxn; k++) 52 { 53 dp[j+1][k]=(dp[j+1][k]+dp[j][k])%mod; 54 if(dp[j][k]) 55 {int gg=gcd(k,v); 56 dp[j+1][gg]=(dp[j+1][gg]+dp[j][k])%mod;} 57 } 58 } 59 for(int j=1; j<=maxn; j++) 60 ans=(ans+j*dp[n][j])%mod; 61 printf("%I64d\n",ans); 62 } 63 } 64 return 0; 65 }
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5350538.html