Codeforces Round #499 (Div. 2)E Border(硬币拼数)

题目链接
题目大意:
n种硬币,每种无限多,问在膜m的情况下能拼出的多少面额。

设可以拼出的面额为x,每种硬币的面额为s1,s2,s3,s4….sn
则x=a1*s1+a2*s2+a3*s3+a4*s4······an*sn+am*m
方程当且仅当x%gcd(s1,s2,s3,s4…sn,m)==0时有解
若有解,又因为am<=0,所以一定存在一组非负解

#include<bits/stdc++.h>
#define ll long long
#define IOS {ios::sync_with_stdio(0);cin.tie(0);}
using namespace std;
const int N = 1e5+1000;
int n,m,i,j;
int main(){
    freopen("a.txt","r",stdin);
    IOS;
    cin>>n>>m;
    int t = m;
    for(i = 1;i <= n;i ++){
        int x;
        cin>>x;
        x%=m;
        if(x==0)continue;
        t = __gcd(t,x);
    }
    if(t==0) {
        cout<<'1'<<endl<<'0';
        return 0;
    }
    int i,ans = 0;
    for(i = 0;i < m;i ++){
        if(i%t==0) ans ++;
    }
    cout<<ans<<endl;
    for(i = 0;i < m;i ++){
        if(i%t==0) cout<<i<<' ';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_30358129/article/details/81236911
今日推荐