Codeforces Round #499 (Div. 2) E. Border 【数学】

http://codeforces.com/contest/1011/problem/E
题目大意是这样的,给出N个面值不同的货币,问这些货币在K进制下能组合成数的尾数有多少个。题意的确有点绕,可以参照样例理解下。

首先题目中的k进制下的“尾数”其实就是模k的值。
所以,我们就将问题转化成这样的等式

a 1 x 1 + a 2 x 2 + a 3 x 3 + . . . + a n x n = m
其中x代表某一个纸币取多少个,m是能形成的余数。
在接这个方程的时候,我们先来了解一下Bézout’s identity:

若a,b是整数,且gcd(a,b)=d,那么对于任意的整数x,y,ax+by都一定是d的倍数,特别地,一定存在整数x,y,使ax+by=d成立。

我们把这个结论推广到多项式中,也就是说m必定是这些数的gcd的倍数,问题解决了。

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <map>
using namespace std;
int main()
{

    int n,k;
    cin>>n>>k;
    int tmp = k;
    for(int i = 1;i<=n;i++)
    {
        int x;
        cin>>x;
        k = __gcd(k,x);
    }
    cout<<tmp/k<<endl;
    for(int i = 0;i<tmp;i+=k)
    {
        cout<<i<<' ';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lingzidong/article/details/81236474