Math (GCD, counting principle) HDU 5656 CA Loves GCD

Source: Internet
Author: User

CA Loves GCDaccepts:135submissions:586Time limit:6000/3000 MS (java/others)Memory limit:262144/262144 K (java/others)Problem description
CA likes to be a love party and people of excellent with ♂, so he also very much like gcd (please enter in the Input method GCD get CA like gcd reason). Now he has n different numbers, and each time he chooses a number (at least one number) to find out all the GCD and put it back. In order to not be bored, the CA will select each of the different options, CA want to know all the gcd he got. We think that the two methods are different when and only if one of the numbers is selected in one of the options, and not selected in the other method.
Enter a description
First lineT, indicating that there areTGroup data. NextT group data, first row of each group of data an integerN, which represents the number of CAs, and the next lineN integersAi?I?? Represents each number of CAs.1≤t≤50, 1≤n≤1000, 1≤ai≤10001 \le T \le 50,~1 \le N \le 1000,~1 \le a_i \le 10001≤T≤50, 1≤N ≤1000 , 1≤ a?i≤1000   
Output description
10000000 7The result of the modulo. 
Input sample
222 431 2 3
Output sample
810
This time the Bestcoder did not hit, has never seen such a difficult second question. We make Dp[i][j] in the first I number, select a number of numbers so that their gcd is the scheme number of J, so only need to enumerate the number of i+1 is selected to transfer it.
The number of i+1 is V, when considering dp[i][j], we make $dp[i+1][j] + + DP[I]J,DP[I+1][GCD (j,v)] + = Dp[i][j]
The Complexity O (N*MAXV) MAXV is the maximum value of the number of occurrences. In fact there is O (maxv *log (MAXV) approach, we consider the memory f[i] to select a number from these numbers, so that their gcd is a multiple of the number of scenarios.  If the number of K is a multiple of I, then f[i]=2^k-1, and then g[i] means to select a number of these numbers, so that their gcd is the scheme number of I, then g[i]=f[i]-g[j] (for all J is a multiple of i). A DP (MB card constant) with a complex degree of O (maxv *log (MAXV)) tle can be obtained by harmonic progression:
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=1010;6 Const intMod=100000007;7 intDP[MAXN][MAXN],A[MAXN];8 intGCD (intAintb) {9     returnB? GCD (b,a%b): A;Ten } One intMain () { A     intT,N,MAXV; -scanf"%d",&T); -      while(t--){ theMemset (DP,0,sizeof(DP)); -scanf"%d", &n); maxv=0; -          for(intI=1; I<=n;maxv=max (Maxv,a[i]), i++) -scanf"%d",&a[i]); +          for(intI=0; i<=n;i++) dp[i][0]=1; -          for(intI=1; i<=n;i++){ +              for(intj=0; j<=maxv;j++) dp[i][j]=dp[i-1][j]; A              for(intj=0; j<=maxv;j++) at(DP[I][GCD (J,a[i])]+=dp[i-1][J])%=MoD; -         } -         intans=0;  -          for(intj=1; j<=maxv;j++) -(ANS+=DP[N][J]*J)%=MoD; -printf"%d\n", ans);  in     } -     return 0; to}

The second method of the AC program:

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=1010;6 Const intMod=100000007;7 Long LongF[maxn],g[maxn],ans;8 intMain () {9     intT,n;Tenscanf"%d",&T); One      while(t--){ Ascanf"%d",&n); -Memset (F,0,sizeof(f)); -          for(intI=1, x;i<=n;i++){ thescanf"%d",&x); -              for(intj=1; j<=x;j++){ -                 if(x%j==0) -f[j]++;  +             } -         } +          for(intI=1; i<= +; i++){ A             Long Longret=1; at              for(intj=1; j<=f[i];j++) -(ret*=2)%=MoD; -ret-=1;  -f[i]=ret; -         } -          for(intI= +; i>=1; i--){ ing[i]=F[i]; -              for(intj=2*i;j<= +; j+=i) to(G[i]-=g[j])%=MoD; +         } -ans=0; the          for(intI=1; i<= +; i++) *(ans+=g[i]*i)%=MoD; $printf"%lld\n", (ans+mod)%MoD); Panax Notoginseng     } -     return 0; the}

Math (GCD, counting principle) HDU 5656 CA Loves GCD

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.