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;
}