Collatz Conjecture
4981: Collatz Conjecture
时间限制: 6 Sec 内存限制: 128 MB
提交: 213 解决: 23
[提交][状态][讨论版][命题人:admin]
题目描述
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.
输入
• A single integer 1 ≤ n ≤ 5 · 105, the length of the sequence.
• The sequence a1, a2, . . . , an. It is given that 1 ≤ ai ≤ 1018.
输出
样例输入
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的更多相关文章
- Python实现Collatz序列(考拉兹猜想)
考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想.3n+1猜想.冰雹猜想.角谷猜想.哈塞猜想.乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1, ...
- dir命令只显示文件名
dir /b 就是ls -f的效果 1057 -- FILE MAPPING_web_archive.7z 2007 多校模拟 - Google Search_web_archive.7z 2083 ...
- 2017 Benelux Algorithm Programming Contest (BAPC 17) Solution
A - Amsterdam Distance 题意:极坐标系,给出两个点,求最短距离 思路:只有两种方式,取min 第一种,先走到0点,再走到终点 第二种,走到同一半径,再走过去 #include ...
- Python高级特性(1):Iterators、Generators和itertools(转)
译文:Python高级特性(1):Iterators.Generators和itertools [译注]:作为一门动态脚本语言,Python 对编程初学者而言很友好,丰富的第三方库能够给使用者带来很大 ...
- gym101666题解
A Amsterdam Distance 题意 求圆环上的两点距离. 分析 显然是沿半径方向走到内圈再走圆弧最短. 代码 #include <bits/stdc++.h> using na ...
- 超实用的 JavaScript 代码片段( ES6+ 编写)
Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...
- saltstack操作
使用分组 修改minon的ID,做一个标识 [root@node2 ~]# vim /etc/salt/minionmaster: 172.25.254.130 id: web1 #node2[roo ...
- Goldbach's Conjecture
Goldbach's Conjecture Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- Debian系统下的ftp服务搭建
安装vsftpd服务 $ sudo apt install vsftpd 配置参数 命令输入 $ vim /etc/vsftpd.conf 使用如下配置 # Example config file / ...
- 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon
P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...
- 用IDEA写出第一个java web
今天学习到如何创建新的java web,笔者使用的是IDEA,以下有详细的参考链接,如下: 参考链接:https://blog.csdn.net/zhdkong/article/details/789 ...
- 黑马Mybatis day3 多表查询 1.xml配置方式 2.注解方式
package com.itheima.mozq; import com.itheima.domain.Order; import com.itheima.mapper.OrderMapper; im ...
- [软件工程基础]Alpha 阶段事后分析
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 帮助选修物理实验的学生撰写实验报告,计算实验数据,验证计算结果,并提供一个讨论的平台. 全体成员认 ...
- C-晾衣服
链接:https://ac.nowcoder.com/acm/contest/892/C 题意: 鸡尾酒从杭州回来,囤积了许多衣服,洗好之后,他发现晾衣服是一件麻烦的事. 晾衣绳的长度只有L,而鸡尾酒 ...
- 洛谷 P3676 小清新数据结构题
https://www.luogu.org/problemnew/show/P3676 这题被我当成动态dp去做了,码了4k,搞了一个换根的动态dp #include<cstdio> #i ...
- Vijos 1002 过河 dp + 思维
https://www.vijos.org/p/1002 设dp[i]表示跳到了第i个点,需要的最小的步数. 所以复杂度O(L * T), 不行 注意到T最大是10, 所以dp[i]最多只由10项递推 ...
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- 渣渣菜鸡的 ElasticSearch 源码解析 —— 环境搭建
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/08/25/es-code01/ 软件环境 1.Intellij Idea:2018.2版本 2. ...