Collatz Conjecture Gym - 101666C

时间: 2023-07-29 admin 互联网

Collatz Conjecture Gym - 101666C

Collatz Conjecture Gym - 101666C

Collatz Conjecture Gym - 101666C

题目大意:

输入一串数目为n的数,求着串数的字串所能组成的最大公约数的不同的个数。 2<=n<=5e5.,1<=a<=1e18.

思路

比赛时没想出好的办法,赛后看大佬的做法,有种前缀和的味道,但又不是。核心思想就是:因为如果你要和前面一个串数比较,那就相当于和前面一个数比较,因为前面一个数已经很它前面所有的数比较过辽。有一种递推的思想。

#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
using namespace std;
#define FOPI freopen("input.in", "r", stdin)
#define DOPI freopen("output.out", "w", stdout)
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 5e5+100;
int lowbit(int x){return x&(-x);}
ll a[maxn];
ll b[maxn];
int main()
{ios::sync_with_stdio(0);set<ll> s;ll r=1;ll n;cin>>n;fro(i,0,n)cin>>a[i];s.insert(a[0]);b[0]=a[0];fro(i,1,n){fro(j,0,r){ll s=gcd(b[j],a[i]);b[j]=s;}b[r++]=a[i];sort(b,b+r);r=unique(b,b+r)-b;fro(k,0,r){s.insert(b[k]);}}cout<<s.size()<<endl;return 0;
}