2022ICPC 网络赛第二场 E An Interesting Sequence

本文探讨了如何构造一个由正整数组成的序列,该序列需满足首项为k,相邻两项互质且总和最小的要求。文章提供了一种有效的算法实现方案,并通过实例演示了解决过程。

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

You should generate a sequence of positive integers of length n.

Of course,this sequence needs to meet some requirements.

  • ∀ i∈[1,n]  ai​>1

  • ∀ i∈[2,n]  gcd(ai−1​,ai​)=1

  • a1​=k(k>1)

gcd(x,y) means the greatest common divisor of x and y.

You need to find the minimum value of

输入格式:

The only line contains two integers n,k

2≤n≤10^8,2≤k≤10^8

输出格式:

One integer — the answer to the problem.

输入样例:

2 5

样例">样例">输出样例:

7

题意: 

给出一个n,k ,求出一串序列,序列的第一个元素为k,序列满足相邻两个数互质,且序列的总和最小,输出总和

题解:

先求出第二个数,第二个数为最小的与k互质的数,如果k为奇数,那么第二个数必定为偶数,那么后面的序列为3232... 如果k为偶数,那么第二个数必定为奇数,那么后面的序列为2323...

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll ;
const int N = 1e8+10;
int n , k ;
int a[2] = {2,3};
int main()
{
    cin >> n >> k ;
    ll res = k ;
    if(k%2){ //第二个数
        res += 2;
    }else{
        for(int i = 3 ; i < k ; i++){
            if(__gcd(k,i)==1){ res += i ; break;}
        }
    } 
    if( k%2 ) //k为奇数
        for(int i = 1 ; i <= n-2; i++ )
            res += a[i%2];
    else //k为偶数
        for(int i = 0 ; i < n-2; i++ )
            res += a[i%2];
    cout << res << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值