Collatz Conjecture(BAPC2017)
Posted jadelemon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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.
• 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\\n",unique(ans,ans+p)-ans); return 0; }
以上是关于Collatz Conjecture(BAPC2017)的主要内容,如果未能解决你的问题,请参考以下文章