HDU 5656 CA Loves GCD DP

Source: Internet
Author: User
Tags gcd greatest common divisor

CA Loves GCD

Time limit:6000/3000 MS (java/others) Memory limit:262144/262144 K (java/others)

Problem Descriptionca is a fine comrade who loves the party and people; Inevitably she loves GCD (greatest common divisor) too.
Now, there isN different numbers. Each time, CA would select several numbers (at least one), and find the GCD of these numbers. In order to has fun, CA would try every selection. After this, she wants to know the sum of all GCDs.
If and only if there are a number exists in a selection, but does isn't exist in another one, we think these and one selections a Re different from all other.

Inputfirst Line containsTDenoting the number of testcases.
TTestcases follow. Each testcase contains an integer in the first time, denotingNThe number of the numbers CA has. The second line isN numbers.
We guarantee the numbers in the test is in the range [1,1000].
1≤T≤

Output T lines, each line prints the sum of GCDs mod 100000007.

Sample Input222 431 2 3

Sample Output810

Sourcebestcoder Round #78 (Div.2)

By YJQ we make dp[i][j] in the first I number, select a number of the number so that their gcd is the number of J scheme, so only need to enumerate the i+1 number is selected to transfer it can be

The number of i+1 is V, when considering dp[i][j], we makeDP[I+1][J] + = Dp[i][j]DP[I+1][J]+=DP[I][J] (v not selected),DP[I+1][GCD (j,v)] + = Dp[i][j]< Span class= "Mord mathit" >dp[i +1][g cd (j,v) ]+< Span class= "Mrel" >=dp[ i][j (v)

Complexity O (N*MAXV) MAXV is the maximum value of the number of occurrences

PS: The number of modulo is the pit point .... It is usually 1e9+7 ...

#include <bits/stdc++.h>using namespacestd;#definell __int64#defineMoD 100000007#defineESP 0.00000000001Const intn=1e3+Ten, m=1e6+Ten, inf=1e9;ll Dp[n][n];intA[n];intFlag[n][n];intgcdintXinty) {    returny==0? X:GCD (y,x%y);}intMain () {intx,y,z,i,t;  for(i=1; i<= +; i++)     for(t=1; t<= +; t++) Flag[i][t]=gcd (i,t); intT; scanf ("%d",&T);  while(t--) {memset (DP,0,sizeof(DP)); intmaxx=0; scanf ("%d",&x);  for(i=1; i<=x;i++) {scanf ("%d",&A[i]); Maxx=Max (A[i],maxx); }         for(i=1; i<=x;i++) {Dp[i][a[i]]+=1;  for(t=1; t<=maxx;t++) {Dp[i][t]+=dp[i-1][t]; Dp[i][t]%=MoD; Dp[i][flag[t][a[i] ]+=dp[i-1][t]; Dp[i][flag[t][a[i] ]%=MoD; }} ll ans=0;  for(LL i=1; i<=maxx;i++) {ans+ = (I*dp[x][i])%MoD; Ans%=MoD; } printf ("%i64d\n", ans); }    return 0;}

HDU 5656 CA Loves GCD DP

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.