This problem can easily be converted to a tolerance counting issue. The enumeration counting method with exponential complexity obviously hangs and can only be considered in other ways.
First the a[i] is replaced with GCD (A[i], M), and the sort is weighed to get an approximate set of M, and M is not more than 1e9, so all the approximate number of M is around 1000.
Assuming that the array a contains only 2, 32 elements, then it is clear that the answer should be f (2) + F (3)-F (6) and F (i) for the case of only I.
The coefficients of 2 and 3 are the coefficients of 1,6 are-1, the remaining coefficients are 0.
According to the principle of repulsion: for a particular approximate number, if there is a subset of array a whose LCM is the approximate, then if the set of elements is even, contribute 1, if it is odd, contribute 1.
The final answer must be synthesized by all approximations with a value of { -1,0,1}.
Therefore, we consider the number of the first m of array A, and its corresponding coefficient distribution can be determined.
When adding the number of m+1, it is obvious that the new set must be obtained by a subset of the original set and a new element, considering all the approximate k of M, with the corresponding coefficients op (k)
If OP (k) is-1, it means that the number of even subsets is 1 more than the number of odd subsets, and if we add a new element x, it just makes the odd subset number 1 more than an even number, but at this point the contribution
Not to K, but to the LCM (k, X), which is represented by a formula:
OP (LCM (k, x)) + = OP (k) * (-1).
The rest of the situation is similar.
The final answer is ans = sigma (OP (i) * f (i)), I | M.
This allows us to perform a maximum of 1000 traversal so that the number of elements is added one and the total complexity is approximately o (1e6).
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <map>5#include <string>6#include <vector>7#include <Set>8#include <cmath>9#include <ctime>Ten#include <cassert> One #defineLson (U << 1) A #defineRson (U << 1 | 1) - #defineCLS (i, J) memset (i, J, sizeof i) - #pragmaComment (linker, "/stack:102400000,102400000") the using namespacestd; - typedef __int64 LL; - Const DoubleEPS = 1e-6; - Const DoublePI = ACOs (-1.0); + Const intMAXN = 1e4 +Ten; - Const intMAXM = 4e4 +Ten; + Const intINF =0x3f3f3f3f; A Constll linf =0x3fffffffffffffff; at Constll mod = 1e9 +7; - -Map<ll,int>MAPI; - intN; - ll A[maxn], m; - intPRIME[MAXM], K; in BOOLVIS[MAXM]; - ll FACT[MAXN], NF; to intCNT[MAXN]; + ll TABLE[MAXN], NT; - intOP[MAXN]; the intOP1[MAXN]; * ll ans; $ Panax Notoginsengll GCD (ll A, ll b) {return!b? A:GCD (b, a%b); } - the voidinit () { +CLS (Vis,0); AK =0; the for(inti =2; i < MAXM; i++){ + if(Vis[i])Continue; -prime[k++] =i; $ for(intj = i *2; J < Maxm; J + = i) vis[j] =1; $ } - } - the voidDfsintNext, ll num) { - if(Next >=NF) {Wuyitable[nt++] =num; theMapi[num] = NT-1; - return; Wu } -ll tem =num; About for(inti =0; I <= Cnt[next]; i++){ $DFS (next +1, TEM); -TEM *=Fact[next]; - } - } A + ll F (ll num) { thell tem = (M-1) /num; - returnTEM * (TEM +1) /2*num; $ } the the voidfactorize () { theNF =0; theLL M1 =m; - intMID = (int) sqrt (m); in for(inti =0; Prime[i] <= mid; i++){ the if(M% prime[i])Continue; thefact[nf++] =Prime[i]; AboutCNT[NF-1] =0; the while(m% prime[i] = =0) ++CNT[NF-1], M/=Prime[i]; theMID = (int) sqrt (m); the } + if(M! =1) fact[nf++] = m, cnt[nf-1] =1; -m =M1; the }Bayi the ll Solve () { the mapi.clear (); - intN1 =0; - for(inti =0; I < n; i++){ thell tem = A[i]%m; the if(!tem)Continue; thea[n1++] =gcd (M, TEM); the } -n =N1; theSort (A, A +n); then = Unique (A, a + N)-A; theCLS (Vis,0);94 for(inti =0; I < n; i++){ the for(intj = i +1; J < N; J + +){ the if(A[i]% a[j] = =0) Vis[i] =1; the Else if(A[j]% a[i] = =0) Vis[j] =1;98 } About } -N1 =0;101 for(inti =0; I < n; i++)if(!vis[i]) a[n1++] =A[i];102n =N1;103Sort (A, A +n);104 factorize (); theNT =0;106Dfs0,1);107CLS (OP,0);108Ans =0;109 for(inti =0; I < n; i++){ theCLS (OP1,0);111 for(intj =0; J < NT; J + +){ thell tem = A[i]/gcd (A[i], table[j]) *Table[j];113 if(TEM >= m)Continue; theOp1[mapi[tem]] + =-Op[j]; the } the++Op1[mapi[a[i]];117 for(intj =0; J < NT; J + +) Op[j] + =Op1[j];118 }119 //for (int i = 0; i < NT; i++) printf ("%d", Op[i]); - //puts ("");121 for(inti =0; I < NT; i++) ans + = op[i] *f (table[i]);122 returnans;123 }124 the intMain () {126 //freopen ("In.txt", "R", stdin);127 intT, Kase =0; - init ();129scanf"%d", &T); the while(t--){131scanf"%d%i64d", &n, &m); the for(inti =0; I < n; i++) scanf ("%i64d", &a[i]);133ll ans =solve ();134printf"Case #%d:%i64d\n", ++Kase, ans);135 }136 return 0;137}
View Code
Hdu5514frogs (2015 Shenyang Division onsite)