D - Border (gcd枚举+裴蜀定理)

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 (1≤n≤1000001≤n≤100000, 2≤k≤1000002≤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 (1≤ai≤1091≤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

2 8
12 20

Output

2
0 4 

Input

3 10
10 20 30

Output

1
0 

Note

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

If you take one banknote with the value of 1212, you will get 148148 in octal system. The last digit is 4848.

If you take one banknote with the value of 1212 and one banknote with the value of 2020, the total value will be 3232. In the octal system, it is 408408. The last digit is 0808.

If you take two banknotes with the value of 2020, the total value will be 4040, this is 508508 in the octal system. The last digit is 0808.

No other digits other than 0808 and 4848 can be obtained. Digits 0808 and 4848 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.

题意:给一个数组,任意选里面的数,求sum%k有多少种情况,n个数能组成的数一定是这n个数的gcd,由扩展欧几里得扩展得来,求gcd注意过滤0,求了n个数gcd还没完,因为可以%,所以还要跟k求gcd

题解:

 给出n种钞票的面值(十进制下),每种钞票数量inf,问在k进制下能组合成的所有面额的最低位的数有哪些。

这就等价于是  a1*x1+a2*x2+......+an*xn=g  ,g就是组合成的钞票面值,由裴蜀定理可知d=y*gcd(a1,a2,,,an),

我们只要求出gcd(a1,a2,,,,,,an),然后把所有情况(y<k)扫一下就好了。


//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/    //博客园
//https://blog.csdn.net/qq_44134712     //csdn

using namespace std;

typedef long long ll;
const ll N=1e5+10;

int a[N];

int main(){
    int n,k;
    cin >> n >> k;
    for(int i=1;i<=n;i++){
        cin >> a[i];
        a[i]%=k;//全部取模
    }
    int m=0;
    for(int i=1;i<=n;i++){
        if(m==0){
            m=a[i];
        }
        else{
            m=__gcd(m,a[i]);//先枚举一遍
        }
    }
//    if(m==0){
//        cout << 1 << endl;
//        cout << 0;
//        return 0;
//    }
    m=__gcd(m,k);
    int num=(k-1)/m;
    cout << num+1 << endl;
    for(int i=0;i<=num;i++){
        if(i==num){
            cout << i*m;
        }
        else{
            cout << i*m << " ";
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值