C - Looooops (欧几里得+裴蜀定理)

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.

输入

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.

输出

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.

样例

Input

2 8
12 20

Output

2
0 4 

Input

3 10
10 20 30

Output

1
0 

--------------------------------------------------------

题意:n个数任意组合所生成的数转化成k进制的尾数的情况 按升序输出所有的情况

思路:

首先一个数转换成k进制数末尾的数为num%k, 由裴蜀定理可知 a1*x1 + a2*x2 +...+ an * xn = c 有整数解,必有c为gcd(a1, a2,...,an)的整数倍, 所以 最后结果就是 i*gcd % k  (0 <= i < k)不同数的集合。

 

 

代码

#include<bits/stdc++.h>
using namespace std;
int exgcd(int a, int b)
{
   if(b==0)
   {
      return a;
   }
   else return exgcd(b, a%b);
}
int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    int y = k;
    for(int i=1;i<=n;i++)
    {
       int x;
       scanf("%d", &x);
       y = exgcd(y, x);
    }
 //   printf("%d\n", y);
    printf("%d\n", k/y);
    int ans = 0;
    while(ans<k)
    {
       printf("%d ", ans);
       ans += y;
    }
    printf("\n");
    return 0;
}

            

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值