-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoprime3.cpp
47 lines (40 loc) · 907 Bytes
/
coprime3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
int gcd(int a,int b){
if(b>a) return gcd(b,a);
return b==0?a:gcd(b,a%b);
}
int T,N;
int A[110];
map<PII,LL> cache;
LL go(int ind,int g){
if(ind==N){
if(g==1) return 1;
else return 0;
}
PII key = make_pair(ind,g);
// cout << key.first << " " << key.second << endl;
if(cache.find(key)!=cache.end()) {
// cout << "cache " << cache[key] << endl;
return cache[key];
}
// cout << "cache2 " << cache[key] << endl ;
return cache[key]=go(ind+1,g)+go(ind+1,gcd(g,A[ind]));
}
int main(){
cin>>T;
while(T--){
cin>>N;
for(int i=0;i<N;++i) cin>>A[i];
cache.clear();
LL res = 0;
for(int i=0;i<N;++i) res+=go(i+1,A[i]);
// go(i+1,A[i]) << endl;
cout<<res<<endl;
}
return 0;
}