Codeforces Round #499 (Div. 2)E. Border(离散数学)

链接:http://codeforces.com/contest/1011/problem/E

题意:给你n种纸币,然后给你一个k,代表k进制,意思就是求这n种纸币的任意组合,然后mod k,能组成多少0~m-1的数字

思路:离散数学的基本知识,n个纸币和m的最大公约数就是最小生成元,这个元的所有倍数小于m的数就是能生成的数。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
#define ll long long
#define eps 1e-8
using namespace std;
inline int read()
{
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n, m, k, x;
int main()
{
    
    n = read(); m = read(); k = m;
    for(int i = 0; i < n; i++)
    {
        x = read();
        k = __gcd(k, x);
    }
    printf("%d\n",m/k);
    for(int i = 0; i < m; i += k)
    printf("%d%c",i, i + k < m ? ' ':'\n');
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/81252325
今日推荐