Codeforces 1011E

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 nn banknote denominations on Mars: the value of ii-th banknote is aiai. Natasha has an infinite number of banknotes of each denomination.

Martians have kk fingers on their hands, so they use a number system with base kk. In addition, the Martians consider the digit dd (in the number system with base kk) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base kk is dd, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.

Determine for which values dd 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 nn and kk (1n1000001≤n≤100000, 2k1000002≤k≤100000) — the number of denominations of banknotes and the base of the number system on Mars.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — denominations of banknotes on Mars.

All numbers are given in decimal notation.

Output

On the first line output the number of values dd 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

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

#include<iostream>
#include<cstdio>
#define maxn 100010
using namespace std;
int n,mod,a[maxn];
bool vis[maxn];
int gcd(int x,int y){
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main(){
    scanf("%d%d",&n,&mod);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=2;i<=n;i++)
        a[i]=gcd(a[i],a[i-1]);
    int ans=0;
    for(int i=1;i<=mod;i++){
        int now=1LL*i*a[n]%mod;
        if(!vis[now]){
            vis[now]=1;
            ans++;
        }
    }
    printf("%d\n",ans);
    for(int i=0;i<=mod;i++){
        if(vis[i])printf("%d ",i);
    }
    puts("");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/thmyl/p/12181417.html