Greatest Common Divisor(差分+gcd)

Greatest Common Divisor

[Link](Problem - G - Codeforces)

题意

给定n个数每次操作可以给所有的数都加一,问你最少操作几次使得这n个数的 g c d > 1 gcd > 1 gcd>1

题解

首先要知道 g c d ( a , b ) = = g c d ( a , b − a ) gcd(a,b)==gcd(a,b-a) gcd(a,b)==gcd(a,ba),设原数组为 a n a_n an原数组的差分数组为 b n − 1 b_{n-1} bn1,设 a n a_n an的最大公约数为 d d d,那么 b n b_n bn的最大公约数应该为 k d kd kd。因为操作是给所有的数加一所以差分数组不变,可以先把差分数组的 g c d gcd gcd求出来假设为 t = k d t=kd t=kd,则差分数组可以表示为 x 1 t , x 2 t , . . . , x n − 1 t x_1t,x_2t,...,x_{n-1}t x1t,x2t,...,xn1t,所以原数组的最大公约数应该是 t t t的一个因数,原数组: a 1 , a 1 + x 1 t , a 2 + x 2 t , . . . , a n − 1 + x n − 1 t a_1,a_1+x_1t,a_2 +x_2t,...,a_{n-1}+ x_{n-1}t a1,a1+x1t,a2+x2t,...,an1+xn1t。发现只需让 a 1 a_1 a1变成 t t t的某个大于一的因数,那么原数组的最大公约数就大约 1 1 1即符合条件,即使得 a 1 = a 1 + ( t 1 − a 1 % t 1 ) a_1=a_1+(t_1-a_1\%t_1) a1=a1+(t1a1%t1),则操作数为 t 1 − a 1 % t 1 t1-a_1\%t_1 t1a1%t1,在所有的合法操作数中取个min即可。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
int n, m, k;
vector<int> a;
vector<int> get(int x) {
    vector<int> divs;
    for (int i = 2; i <= x / i; i ++ ) {
        if (x == 1) break;
        if (x % i == 0) {
            divs.push_back(i);
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) divs.push_back(x);
    return divs;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T, C = 0;
    cin >> T;
    while (T -- ) {
        a.clear();
        cin >> n; bool ok = false;
        for (int i = 0; i < n; i ++ ) {
            int x; cin >> x;
            a.push_back(x);
        }
        sort(a.begin(), a.end());
        a.erase(unique(a.begin(), a.end()), a.end());
        int pre = a[0], d = 0;
        if (a.size() == 1) {
            if (a[0] - 1)  cout << "Case "<< ++ C << ": " << 0 << endl;
            else  cout << "Case "<< ++ C << ": " << 1 << endl;
            continue ;
        }
        for (int i = 1; i < a.size(); i ++ ) {
            d = gcd(d, a[i] - pre);
            if (d == 1) {
                ok = true;
                break;
            }
            pre = a[i];
        }
       // cout << d << endl;
        if (ok) {
            cout << "Case "<< ++ C << ": " << -1 << endl;
            continue ;
        }
        vector<int> divs = get(d);
        int res = INF;
        for (auto t : divs) {
            if (a[0] % t == 0) res = 0;
            res = min(res, t - a[0] % t);
        }
        cout << "Case "<< ++ C << ": " << res << endl;
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值