C Collatz Conjecture

C Collatz Conjecture

In 1978 AD the great Sir Isaac Newton, whilst proving that Pis a strict superset of N P, defined the Beta Alpha Pi Zetafunction f as follows over any sequence of positive integersa1, . . . , 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 tothe sequence 1, 1, 1, . . . , 1, and observed that f always equalled1. Based on this, he conjectured that f is always a constantfunction, no matter what the sequence aiis. This conjecture, now widely known as theCollatz Conjecture, is one of the major open problems in botanical studies. (The StrongCollatz Conjecture claims that however many values f takes on, the real part is always 12.)

You, a budding young cultural anthropologist, have decided to disprove this conjecture. Givena sequence ai, calculate how many different values f takes on.

Input

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

Output a single line containing a single integer, the number of distinct values f takes on overthe given sequence.


思路:计算每个以i结尾的子串的gcd。每次用in[]记录,若gcd不重复加入到s[]中。最后去重。

代码:

#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;

LL in[1000010]; //前i位最大公约数
LL s[10000010];
   //枚举右端点,找出以i结尾之前所有不同的gcd,ans记录出现过的不同的gcd。

LL gcd(LL a,LL b)
{
    return a==0?b:gcd(b%a,a);
}

int main()
{
    int n;
    LL x;
    cin>>n;
    int top=0;
    int k=0;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        for(int j=0;j<top;j++)  //求以x结尾的gcd
        {
            LL y=gcd(x,in[j]);  //a1a2最大公约数in[j],求a1a2a3就是求gcd(a3,in[j])
            if(y!=in[j])
            {
                s[k++]=in[j];
                in[j]=y;
            }
        }
        in[top++]=x;
        top=unique(in,in+top)-in;  //去重
    }
    for(int i=0;i<top;i++)
        s[k++]=in[i];
    sort(s,s+k);
    cout<<unique(s,s+k)-s<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值