Codeforces Round #499 (Div. 2) ---- E Border

D题确实没看懂,跑来做了E。题目中给了我们一些数字,我们可以拿这些数字,每个不定量。拿到的数转成k进制的最后一位不同的个数有多少。

我们假设每个数字拿了xi个,那么总和就是

                                             ( a1 x1 + a2 x2 +a3 x3 + a4 x4 + a5 x5 + ... + an xn ) % k = ans ;

其中,这个方程的和有一个特殊的性质,一定是a1 a2 ... an  的 gcd的倍数,这样的方程是有解的,那么我们算出他们的gcd,枚举它的倍数这样的数都是答案的一种,那么我们枚举到出现重复的mod数的时候停止即可。

代码如下

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int gcd(int a,int b){if (b == 0) return a; return gcd(b , a%b);}
int lcm(int a, int b){ return a/gcd(a,b)*b;}
const int maxn = 1e6 + 10;
int vis[maxn];
int main(){
    int n,k,num1,num2,base;
    cin >> n >> k >> num1;
    if (n == 1){
        base = num1;
    }else{
        cin >> num2;
        base = gcd(num1, num2);
    }
    for (int i=0; i<n-2; i++) {
        scanf("%d",&num1);
        base = gcd(num1,base);
    }
    int cnt = 1;
    while (1) {
        LL now = (LL) base * cnt % k;
        if (vis[now]) break;
        else vis[now] = 1;
        cnt++;
    }
    cout << cnt -1 << endl;
    for (int i=0; i<maxn; i++) {
        if (vis[i]) {
            cout << i << ' ';
        }
    }
    cout << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CCCCTong/article/details/81261569