Codeforces Round #499 (Div. 2) E. Border (GCD的应用)

原题地址:http://codeforces.com/contest/1011/problem/E

题意:给出n个数,进制为k,然后每个数字可以取无数次,现在问你任意组合的数字在k进制下,他模上k后的数字的种类有多少,输出数量并输出结果.

思路:考虑不同数字之间的gcd,如果gcd为1,那么一定是所有小于k的数字都能取到,同样的道理,如果如果gcd不为1,那么结果就是gcd的倍数.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
int a[maxn];
int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    int x = k;
    for(int i = 1; i <= n; i++) {
        x = __gcd(x, a[i]);
    }
    printf("%d\n",  k / x);
    int cnt = 0;
    while(cnt < k) {
        printf("%d ", cnt);
        cnt += x;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/81235911
今日推荐