Codeforces Collatz Conjecture(思维)

本文介绍了一种算法,用于解决计算给定整数序列中所有可能子序列的最大公约数(GCD)种类的问题。该算法通过维护两个哈希集合来记录已计算的GCD值,并在遍历过程中更新这些值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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.

Input

The input consists of two lines.
• A single integer 1 ≤ n ≤ 5105 5 · 10 5 , 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 over the given sequence.

Sample

input
4
9 6 2 4
output
6
input
4
9 6 3 4
output
5

题目描述

求任意子序列的gcd数有多少种.

解题思路

tmp表示当前数字与前面数字的所有的gcd.

代码实现

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
unordered_set<ll>ans;
unordered_set<ll>tmp1;
unordered_set<ll>tmp2;
unordered_set<ll>::iterator it;
int main()
{
    IO;
    int n;
    ll x;
    cin>>n;
    cin>>x;
    tmp1.insert(x);
    ans.insert(x);
    for(int i=1;i<n;i++)
    {
        cin>>x;
        tmp2.insert(x);
        for(auto it:tmp1)
        {
            ll gcd=__gcd(it,x);
            tmp2.insert(gcd);
        }
        tmp1.clear();
        for(auto it:tmp2)
        {
            if(it!=1)
                tmp1.insert(it);
            ans.insert(it);
        }
        tmp2.clear();
    }
    cout<<ans.size()<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值