Border Codeforces Round #499 (Div. 2) (数论)

E. Border
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.

There are n
banknote denominations on Mars: the value of i-th banknote is ai

. Natasha has an infinite number of banknotes of each denomination.

Martians have k
fingers on their hands, so they use a number system with base k. In addition, the Martians consider the digit d (in the number system with base k) divine. Thus, if the last digit in Natasha’s tax amount written in the number system with the base k is d

, the Martians will be happy. Unfortunately, Natasha does not know the Martians’ divine digit yet.

Determine for which values d

Natasha can make the Martians happy.

Natasha can use only her banknotes. Martians don’t give her change.
Input

The first line contains two integers n
and k (1≤n≤100000, 2≤k≤100000

) — the number of denominations of banknotes and the base of the number system on Mars.

The second line contains n
integers a1,a2,…,an (1≤ai≤109

) — denominations of banknotes on Mars.

All numbers are given in decimal notation.
Output

On the first line output the number of values d

for which Natasha can make the Martians happy.

In the second line, output all these values in increasing order.

Print all numbers in decimal notation.
Examples
Input
Copy

2 8
12 20

Output
Copy

2
0 4

Input
Copy

3 10
10 20 30

Output
Copy

1
0

Note

Consider the first test case. It uses the octal number system.

If you take one banknote with the value of 12
, you will get 148 in octal system. The last digit is 48

.

If you take one banknote with the value of 12
and one banknote with the value of 20, the total value will be 32. In the octal system, it is 408. The last digit is 08

.

If you take two banknotes with the value of 20
, the total value will be 40, this is 508 in the octal system. The last digit is 08

.

No other digits other than 08
and 48 can be obtained. Digits 08 and 48

could also be obtained in other ways.

The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero.

题意
(a1x+a2y+a3z+……..anw ) %k的种类数
思路:
我们知道 a1x+a2y+a3z+……..anw = d 当且仅当gcd(a1,a2,a3,…an) | d
所以这题等价于 (xgcd(a1,a2,a3,…an))%k 的种类数 枚举下就行
accode

#include<bits/stdc++.h>
#define LL long long
#define INF  0x3f3f3f3f
using namespace std;
const int maxn = 1e5+32;
LL n,k;
LL a[maxn];
LL ans[maxn];
int main()
{
    scanf("%lld%lld",&n,&k);
    for(int i = 1;i<=n;i++){
        scanf("%lld",&a[i]);
       a[i]%=k;
    }

    LL gcd = a[1];
    for(int i = 2;i<=n;i++){
        gcd = __gcd(gcd,a[i]);
    }
    int p = 0;
    ans[p++] =0;
    LL now = gcd;
    LL nowx = 1;
    while(nowx<=k&&gcd!=0){
        now=nowx*gcd%k;
        if(nowx>k){break;}
        ans[p++] = now;
        nowx++;
    }
    sort(ans,ans+p);
    int len = unique(ans,ans+p)-ans;
    cout<<len<<endl;
    for(int i = 0;i<len;i++){
        printf("%lld%c",ans[i],i==len-1?'\n':' ');
    }
}

猜你喜欢

转载自blog.csdn.net/w571523631/article/details/81237660