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. 基于zookeeper实现分布式配置中心(一)

    最近在学习zookeeper,发现zk真的是一个优秀的中间件.在分布式环境下,可以高效解决数据管理问题.在学习的过程中,要深入zk的工作原理,并根据其特性做一些简单的分布式环境下数据管理工具.本文首先 ...

  2. JS中map list 数组的迭代

    后台传给前台一个map 前台如何迭代呢 $.post("getSys.jhtml", function(data){ var temp = ""; $.each ...

  3. Git 分支管理 Feature分支 强行删除分支

    软件开发中,总有无穷无尽的新的功能要不断添加进来. 添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了, 所以,每添加一个新功能,最好新建一个feature分支, 在上面开发,完成后 ...

  4. linux端口netstat

    netstat -aptn命令行,查看所有开启的端口号 netstat -nupl       查看所有udp端口号 netstat -ntpl   查看所有tcp端口号 查看某服务占用的端口情况,比 ...

  5. spring-eureka 源码解读----为什么一个服务最多两分钟被其他服务感知

    Eureka的wiki上有一句话,大意是一个服务启动后最长可能需要2分钟时间才能被其它服务感知到,但是文档并没有解释为什么会有这2分钟.其实这是由三处缓存 + 一处延迟造成的. 首先,Eureka对H ...

  6. 大整数因子(高精mod)

    大整数的因子 总时间限制:  1000ms 内存限制:  65536kB 描述 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k. 输入 一个非 ...

  7. css中vw和vh的知识点

    引用文档:http://caibaojian.com/vw-vh.html: http://www.zhangxinxu.com/wordpress/2012/09/new-viewport-rela ...

  8. Netty(2)Echo

    上节介绍的是discard协议,即不给客户端返回消息.本节主要说下,echo协议,即服务端收到消息后原样返回给客户端. 为了实现此需求,只需要在DiscardServerHandler中重写chann ...

  9. NET Core源代码通过Autofac实现依赖注入

    查看.NET Core源代码通过Autofac实现依赖注入到Controller属性   阅读目录 一.前言 二.使用Autofac 三.最后 回到目录 一.前言 在之前的文章[ASP.NET Cor ...

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

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