BAPC2017 UPC Collatz Conjecture (GCD, 骚操作)

Collatz Conjecture

时间限制: 6 Sec  内存限制: 128 MB
提交: 160  解决: 12
[提交][状态][讨论版][命题人:admin]

题目描述

In 1978 AD the great Sir Isaac Newton, whilst proving that P is a strict superset of N P, defined the Beta Alpha Pi Zeta function f as follows over any sequence of positive integers a1,..., an. Given integers 1 ≤ i ≤ j ≤ n, we define f(i, j) as gcd(ai, ai+1,..., aj−1, aj).
About a century later Lothar Collatz applied this function to the sequence 1, 1, 1,..., 1, and observed that f always equalled 1. Based on this, he conjectured that f is always a constant function, no matter what the sequence ai is. This conjecture, now widely known as the Collatz Conjecture, is one of the major open problems in botanical studies. (The Strong Collatz Conjecture claims that however many values f takes on, the real part is always 1/2 .)
You, a budding young cultural anthropologist, have decided to disprove this conjecture. Given a sequence ai, calculate how many different values f takes on.

输入

The input consists of two lines.
• A single integer 1 ≤ n ≤ 5 · 105, the length of the sequence.
• The sequence a1, a2, . . . , an. It is given that 1 ≤ ai ≤ 1018.

输出

Output a single line containing a single integer, the number of distinct values f takes on over the given sequence. 

样例输入

4

9 6 2 4

样例输出

6

提示

来源


[思路]

 求 不重复的GCD 的个数,   每次操作  都会出现大量的重复运算;

与 https://blog.csdn.net/sizaif/article/details/79624438   题 操作类似;

不过这道题显然时间复杂度卡的更高,数据量更大;

所以对于去重操作,不能一个一个的操作,  用一个 unique  函数;

unique 去重操作,  必须保证是有序的才可以;  返回的是地址;

一个gc 暂存gcd ,  ans 存全部的,     最后ans 排序, 然后 去重


【code】 

/*
* Date:4/8/2018
* Tile:UPC 补
* Category :GCD 数论
* Attention : 注意复杂度, 卡时间,数据量大
*/
#include <iostream>
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const ll INF=0x3f3f3f3f;
const int MAXN=6e5+5;
const int MOD=1e9+7;

using namespace std;

ll gc[MAXN];
ll a[MAXN];
ll ans[MAXN];

int main()
{
    ll n;

    ll k=0,cot=0;
    scanf("%lld",&n);
    for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<k;j++)
        {
            ll x =__gcd(a[i],gc[j]);
            if(x!=gc[j])
            {
                ans[cot++]=gc[j];
                gc[j]=x;
            }
        }
        gc[k++]=a[i];
        k=unique(gc,gc+k)-gc;
    }
    for(int i=0;i<k;i++)
        ans[cot++]=gc[i];
    sort(ans,ans+cot);
    ll res= unique(ans,ans+cot)-ans;
    cout<<res<<endl;
    return 0;
}

posted @ 2018-04-08 21:53  Sizaif  阅读(330)  评论(0编辑  收藏  举报