zoukankan      html  css  js  c++  java
  • Collatz Conjecture(BAPC2017)

    问题 B: Collatz Conjecture

    时间限制: 6 Sec  内存限制: 128 MB
    提交: 163  解决: 13
    [提交][状态][讨论版][命题人: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的个数,与bzoj 4488很相似,运用了同一个规律,唉,之前写的很不好,用了map容器老是超时,然后看了看大佬的代码,发现好优化,只建2个数组,数组in[]代表a[1:i]的后缀的不同的gcd,数组ans[]表示所有的gcd,a[i]一个一个往里加,每次维护in[]数组.最后sort(ans)去重,求ans的大小

    c++ code:
    #include<bits/stdc++.h>
     
    using namespace std;
    const int NMAX=1e6+7;
    typedef long long ll;
    ll in[NMAX],ans[10*NMAX];
    ll gcd(ll a,ll b){
        return b==0?a:gcd(b,a%b); 
    }
    int top,p;
    int main()
    {
        int n;
        ll x,y;
        top=p=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&x);
            for(int j=0;j<top;j++)
            {
                y=gcd(x,in[j]);
                if(y!=in[j])
                {
                    ans[p++]=in[j];
                    in[j]=y;
                }
            }
            in[top++]=x;
            top=unique(in,in+top)-in;
        }
        for(int i=0;i<top;i++)
            ans[p++]=in[i];
        sort(ans,ans+p);
        printf("%d
    ",unique(ans,ans+p)-ans);
        return 0;   
    } 
  • 相关阅读:
    675 对象的引用-浅拷贝-深拷贝
    674 vue3侦听器watch
    673 vue计算属性:缓存,setter和getter
    明明有了promise,为啥还需要async await?
    Js常用数组方法汇总
    一些非常有用的Js单行代码
    Js获取验证码倒计时
    前端截取字符串:JS截取字符串之substring、substr和slice详解
    javascript全局变量与局部变量
    JS实现快速排序算法
  • 原文地址:https://www.cnblogs.com/lemon-jade/p/8748551.html
Copyright © 2011-2022 走看看