Codeforces Round #511 (Div. 2), problem: (C) Enlarge GCD 数论
程序员文章站
2022-03-02 23:05:55
...
思路大体是先找到an取值范围内的质数并保存,求出输入的数据中最大公倍数并将a数组都除以最大公倍数,然后遍历an的取值范围(从2开始,1不算),如果是质数就统计这个质数和它的倍数,就是使最小公倍数增加所需要去掉的数的个数,遍历过程中取最小值即可。并注意判定特殊情况。这个方法几乎要超时了似乎有更好的办法,但是我没想到。
ac代码:
#include <bits/stdc++.h>
#define FOR(I,A,B) for(int I = (A); I < (B); I++)
#define FORE(I,A,B) for(int I = (A); I <= (B); I++)
#define PRII pair<int,int>
#define LL long long
#define INF 1000000001
#define N 200005
#define M 15000000
int gcd(int a,int b) { return b?gcd(b,a%b):a;}
using namespace std;
int a[300005];
int k[M],ss[M];
int main()
{
int n;
int ans = INF;
cin >> n;
cin >> a[1] >> a[2];
bool fy = true;
int agcd = gcd(a[1],a[2]);
if(a[1] != a[2]) fy = false;
FORE(i,3,n){
cin >> a[i];
agcd = gcd(a[i],agcd);
if(a[i] != a[i-1]) fy = false;
}
FORE(i,2,int(sqrt(M)+1)){
if(ss[i] == 0){
for(int j = i*2;j <= M;j+=i) ss[j] = 1;
}
}
FORE(i,1,n){
k[a[i]/agcd]++;
}
推荐阅读
-
Codeforces Round #320 (Div. 2) C. A Problem about Polyline ( 数学 )
-
Orac and LCM(GCD/LCM/质因子分解/推导证明)Codeforces Round #641 (Div. 2) C题
-
Educational Codeforces Round 86 (Rated for Div. 2)C. Yet Another Counting Problem
-
Codeforces Round #555 (Div. 3), problem: (C2) Increasing Subsequence (hard version)【贪心+撞到南墙也不回头】
-
Codeforces Round #511 (Div. 2)C. Enlarge GCD
-
Codeforces Round #511 (Div. 2) C. Enlarge GCD(思路,数论)
-
Codeforces Round #511 (Div. 2) C - Enlarge GCD (数论, 暴力)
-
Codeforces Round #511 (Div. 2) C. Enlarge GCD
-
【数论思维题】Enlarge GCD【Codeforces Round #511 (Div. 2)】
-
Codeforces Round #511 (Div. 2) C - Enlarge GCD(筛法)