Guest User

Untitled

a guest
Jun 23rd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. int a[100],n,dp[61][10001];
  4. ll solve(int index , ll gcd){
  5.     ll ans1,ans2;
  6.     if(index==0)    return 0;
  7.     if(dp[index][gcd])  return dp[index][gcd];
  8.     ans1 = solve(index-1,gcd);
  9.     if(gcd==0){
  10.         ans2=solve(index-1,a[index]);
  11.         ans2+=a[index]==1?1:0;
  12.     }
  13.     else{
  14.         ans2=solve(index-1,__gcd(gcd,(ll)a[index]));
  15.         ans2+=__gcd(gcd,(ll)a[index])==1?1:0;
  16.     }
  17.     dp[index][gcd] = ans1+ans2;
  18.     return ans1 + ans2;
  19. }
  20. int main(){
  21. #ifndef ONLINE_JUDGE
  22.     freopen("input.txt", "r", stdin);
  23.     freopen("output.txt", "w", stdout);
  24. #endif
  25.     // faster
  26.     int t;
  27.     cin>>t;
  28.     while(t--){
  29.         for(int i=1;i<=60;i++)
  30.             for(int j=0;j<=4000;j++)
  31.                 dp[i][j]=0;
  32.         cin>>n;
  33.         f(n)    cin>>a[i+1];
  34.         ll ans = a[n]==1?1:0;
  35.         ans = solve(n-1,a[n]) + solve(n-1,0) ;
  36.         cout<<ans<<"\n";
  37.     }  
  38. return 0;  
  39. }
Add Comment
Please, Sign In to add comment