4981: Collatz Conjecture

时间限制: 6 Sec  内存限制: 128 MB
提交: 213  解决: 23
[提交][状态][讨论版][命题人: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,代码很好理解!
AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll a[MAXN],g[MAXN],ans[10*MAXN];
int main()
{
ll n;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>a[i];
}
ll tot=0;
ll cot=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<tot; j++)
{
ll x=gcd(a[i],g[j]);
if(x!=g[j])
{
ans[cot++]=g[j];
g[j]=x;
}
}
g[tot++]=a[i];
tot=unique(g,g+tot)-g;
}
for(int i=0; i<tot; i++)
{
ans[cot++]=g[i];
}
sort(ans,ans+cot);
ll ret=unique(ans,ans+cot)-ans;
cout<<ret<<endl;
return 0;
}

Collatz Conjecture的更多相关文章

  1. Python实现Collatz序列(考拉兹猜想)

    考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想.3n+1猜想.冰雹猜想.角谷猜想.哈塞猜想.乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1, ...

  2. dir命令只显示文件名

    dir /b 就是ls -f的效果 1057 -- FILE MAPPING_web_archive.7z 2007 多校模拟 - Google Search_web_archive.7z 2083 ...

  3. 2017 Benelux Algorithm Programming Contest (BAPC 17) Solution

    A - Amsterdam Distance 题意:极坐标系,给出两个点,求最短距离 思路:只有两种方式,取min  第一种,先走到0点,再走到终点 第二种,走到同一半径,再走过去 #include ...

  4. Python高级特性(1):Iterators、Generators和itertools(转)

    译文:Python高级特性(1):Iterators.Generators和itertools [译注]:作为一门动态脚本语言,Python 对编程初学者而言很友好,丰富的第三方库能够给使用者带来很大 ...

  5. gym101666题解

    A Amsterdam Distance 题意 求圆环上的两点距离. 分析 显然是沿半径方向走到内圈再走圆弧最短. 代码 #include <bits/stdc++.h> using na ...

  6. 超实用的 JavaScript 代码片段( ES6+ 编写)

    Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...

  7. saltstack操作

    使用分组 修改minon的ID,做一个标识 [root@node2 ~]# vim /etc/salt/minionmaster: 172.25.254.130 id: web1 #node2[roo ...

  8. Goldbach's Conjecture

     Goldbach's Conjecture Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  9. Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)

    Twin Prime Conjecture                                            Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. python读写xlsx

    1使用openpyxl库读写excel xlrd和xlwt处理的是xls文件,单个sheet最大行数是65535,如果有更大需要的,建议使用openpyxl函数,最大行数达到1048576.  如果数 ...

  2. FString转到char* TCHAR*

    int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName) { return GetColumnIndex(resu ...

  3. python 模拟事件触发机制

    EventManager.py # -*- encoding: UTF-8 -*- # 系统模块 from queue import Queue, Empty from threading impor ...

  4. mac for Idea配置Tomcat

    参考链接地址:http://blog.csdn.net/zsy3313422/article/details/52583091 IntelliJ使用 ##使用IntelliJ IDEA配置web项目时 ...

  5. java基础第十一篇之Date、Math、自动装箱和拆箱

    Date类 表示一个瞬间,就是一个时刻 * * 构造方法: * public Date();//创建一个表示当前系统时间的Date对象 * public Date(long time);//毫秒值,距 ...

  6. Javascript中常用方法简介

    Array数组常用方法       先创建一个数组var abc = [1,2,3,4,5,6,7,8,9]; (1)pop(); 这个方法会删除数组的最后一项并返回删除掉的值. 比如:console ...

  7. Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS

    链接:https://codeforces.com/contest/1167/problem/D 题意: A string is called bracket sequence if it does ...

  8. TPU的相关资料

    1.谷歌 TPU 的强大与局限:TPU/GPU/CPU性能功耗全面对比:http://www.sohu.com/a/134315786_473283 2.谷歌发布TPU论文,75位联合作者,GPU迎来 ...

  9. POJ 3735 Training little cats 矩阵快速幂

    http://poj.org/problem?id=3735 给定一串操作,要这个操作连续执行m次后,最后剩下的值. 记矩阵T为一次操作后的值,那么T^m就是执行m次的值了.(其实这个还不太理解,但是 ...

  10. C#远程连接sqlserver时,尝试读取或写入受保护的内存

    管理员身份运行 cmd ->  输入 netsh winsock reset