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. 多列组合为主键(PRIMARY KEY)

    在表中,想把其中2列或多列作为组合主键. CREATE TABLE [dbo].[T3] ( ) NOT NULL, ) NOT NULL, ) NULL, ) NULL ) GO ALTER TAB ...

  2. debian 7上源码编译MongoDB 3.4版本

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 要想精通一个数据库,除了知道该数据库的功能特性.使用方法等,还需要能够看懂数据库源码,能够使用gdb工具对其 ...

  3. 【工具篇】Sublime Text 2/3 安装汉化破解、插件包安装教程详解

    Sublime Text概述: Sublime Text是一个代码编辑器,也是HTML和散文先进的文本编辑器. 漂亮的用户界面和非凡的功能,例如:迷你地图,多选择,Python插件,代码段等等. 完全 ...

  4. python接口自动化(三十九)- logger 日志 - 上(超详解)

    简介 Python的logging模块提供了通用的日志系统,可以方便第三方模块或者是应用使用.这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP GET/POST,SMTP, ...

  5. Web自动化测试—PO设计模式(二)

    PO设计模式要点一:页面类都继承于BasePage 目录结构 ui_auto_test --src --pages --__init__.py --base_page.py --login_page. ...

  6. 使用CSS3的translate和transition功能,控制一个两个div块的联动

    之前的工作有接触到一些css3的新特性.div块的移动和回到初始位置,可以利用在开发中的很多地方.这里记录下来,下次就不用辛苦的灾区百度了. <html> <head> < ...

  7. NET full stack framework

    NFX UNISTACK 介绍 学习.NET Core和ASP.NET Core,偶然搜索到NFX UNISTACK,现翻译一下Readme,工程/原文:https://github.com/aumc ...

  8. GPIO的翻转操作方法

    STM32在进行IO翻转操作的时候可以使用以下方法:以PE.5为例 GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(1-(GPIO_ReadOutputDataB ...

  9. Vue2之页面 、js 、css分离

    在编写vue的时候,页面数据少的时候,可以将所有的js和css都可以直接写在页面上,但是页面数据多,js.css的方法和样式多的时候,都放在一个页面的时候,就显得页面vue十分的臃肿. 所以写项目的时 ...

  10. MyBatis配置文件之概述

    MyBatis配置文件所有元素 <?xml version="1.0" encoding="UTF-8"?> <configuration&g ...