hdu-5656 CA Loves GCD (dp+ number theory)

Source: Internet
Author: User
Tags greatest common divisor

Topic Links:

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 Test Instructions:Give n number, ask n number of the total combination of GCD and is how much; Ideas:Note that the number is within 1000, so the combination of all to find out, but the number is huge so the same to be compressed; AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;ll dp[1003],num[1003],p[1003][1003];intn,a[1003];Constll mod=1e8+7;//Note is 8, the time of the game this place direct wa to cryintgcdintXinty) {    if(y==0)returnx; returnGCD (y,x%y);}intMain () { for(intI=1; i<= +; i++)    {         for(intj=i;j<= +; j + +) {P[i][j]=gcd (I,J); }    }    intT; scanf ("%d",&t);  while(t--)    {         for(intI=1; i<= +; i++) {Num[i]=0; } ll ans=0; scanf ("%d",&N);  for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); } sort (A+1, a+n+1);  for(intI=1; i<=n;i++) {Dp[i]=(LL) a[i];  for(intj=1; j<=a[i];j++)            {                if(Num[j]) {Num[j]%=MoD; Dp[i]+=num[j]*p[j][a[i]];//Compressed Place dp[i]%=MoD; Num[p[j][a[i] ]+=Num[j]; Num[p[j][a[i] ]%=MoD; }} Num[a[i]]++; Ans+=Dp[i]; Ans%=MoD; } cout<<ans<<"\ n"; }    return 0;}

hdu-5656 CA Loves GCD (dp+ number theory)

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.