4981: Collatz Conjecture

2024-03-03 04:48
文章标签 collatz conjecture 4981

本文主要是介绍4981: Collatz Conjecture,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

4981: Collatz Conjecture

题目描述

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

枚举右端点,找出以i结尾之前所有不同的gcd,ans记录出现过的不同的gcd。

temp是递增的可以直接用unique。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);
}
ll ans[7000005],temp[500005],x;
int main()
{int n,m=0,k=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lld",&x);for(int j=0;j<m;j++){ll t=gcd(temp[j],x);if(t!=temp[j]){ans[k++]=temp[j];temp[j]=t;}}temp[m++]=x;m=unique(temp,temp+m)-temp;}for(int i=0;i<m;i++)ans[k++]=temp[i];sort(ans,ans+k);int sum=1;for(int i=1;i<k;i++)if(ans[i]!=ans[i-1])sum++;printf("%d\n",sum);return 0;
}

这篇关于4981: Collatz Conjecture的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/768491

相关文章

【ZOJ】3881 From the ABC conjecture【暴力容斥】

传送门:【ZOJ】3881 From the ABC conjecture 复杂度大概 O(N0.67) O(N^{0.67}),我也不会算www,首先转换一下(我们是根据积性函数打表找规律得到的,也可以推出来)使得: g(N)=∏pi ϵ N(pia+1) g(N)=\prod_{pi~\epsilon ~N} (pi^a+1) 暴力展开发现贡献为: h(N)=

POJ2262.Goldbach's Conjecture(哥德巴赫的猜想)

【题意】 哥德巴赫猜想:大于四的偶数可以分解为两个奇素数之和 对于给出的数n,如果有多对奇数素数加起来为n,则选择差值b-a最大化的。 若没有这样的一对,则打印"Goldbach's conjecture is wrong." 【思路】 枚举:直接枚举所有的,看是否符合条件,从小到大枚举到中间的话符合条件的也就是差值最大的 #include<stdio.h> #include<string

hdu 4981 Goffi and Median(水题)

题目链接:hdu 4981 Goffi and Median 题目大意:给定一个序列,判断中位数是否比平均值大。 解题思路:水题。排个序求中位数比较一下。 #include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1005;int N, arr[maxn];

HDU 4981 Goffi and Median(水)

HDU 4981 Goffi and Median 思路:排序就可以得到中间数,然后总和和中间数*n比较一下即可 代码: #include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int N = 1005;int n, a[N], sum

LightOJ 1259 Goldbach`s Conjecture

题意: 给出一个偶数n,求出有几对素数的和等于n;即素数a,b其中(a <=b < n)    问有几对a+b = n。 分析: 一开始用素数打表,开个1e7的int数组存是否为素数, 最后再遍历 这些素数,是否存在 b =(n-这个数),  如果b还是素数,并且b >= a,则存在一组,ans++。 一开始内存超限了几次,后来学到了,开的数组可以开个bool类型的,bool 每一个只占一个字节,

Collatz 序列(考拉咨猜想),用Python自动化无聊的东西-chapter3

编写一个名为的函数collatz(),它有一个名为的参数number。如果number是偶数,那么collatz()应该打印number // 2并返回这个值。如果number是奇数,collatz()则应打印并返回3 * number + 1。 然后编写一个程序,让用户键入一个整数,并持续调用collatz()该数字,直到函数返回值1。(很奇怪,这个序列实际上适用于任何整数 - 早或晚,使用这

694 - The Collatz Sequence

Step 1: Choose an arbitrary positive integer A as the first item in the sequence. Step 2: If A = 1 then stop. Step 3: If A is even, then replace A by A / 2 and go to step 2. Step 4: If A is odd, t

数论-质数-Goldbach's Conjecture

水题。 #include<bits/stdc++.h>#define rep(i,l,r) for(int i=(l);i<=(r);i++)#define per(i,r,l) for(int i=(r);i>=(l);i--)#define random(l,r) ((l)+rand()%((r)-(l)+1))using namespace std;typedef unsigne

【HDU】 1397 Goldbach's Conjecture

Goldbach’s Conjecture 题目链接 Goldbach’s Conjecture 题目大意     给你一个偶数,让你去计算有多少组不同的质数相加等于这个偶数。     比如10=5+5、10=3+7,所以10的答案是2. 题解     直接打表暴力了…因为数据很小嘛 代码 #include <iostream>#include <cstring

BAPC2017 UPC Collatz Conjecture (数论GCD, 去重优化)

4981: Collatz Conjecture 时间限制: 6 Sec   内存限制: 128 MB 提交: 210   解决: 21 [ 提交][ 状态][ 讨论版][命题人: admin] 题目描述 In 1978 AD the great Sir Isaac Newton, whilst proving that P is a strict superset of