codeforces 1010 C. Border Bézout's identity

C. 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≤?≤1000001≤n≤100000, 2≤?≤1000002≤k≤100000) — the number of denominations of banknotes and the base of the number system on Mars.

The second line contains ?n integers ?1,?2,…,??a1,a2,…,an (1≤??≤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 ?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 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.

 

题意:给n种数,n种数取任意个任意组合相加为sum,求sum%k有哪些值。

 

 

题解:

Note that the condition "the last digit in the record of Natasha's tax amount in the number system with the base ?k will be ?d" is equivalent to the condition "the remainder of dividing the tax on ?k will be ?d".

Let ?=???(?1,?2,…,??)g=GCD(a1,a2,…,an). It is stated that the original problem is equivalent to the problem where ?=1n=1 and the only banknote is ?g.

Evidence. We prove this with the help of the Bézout's identity. It follows that an equation of the form ?1a1?1+?2x1+a2?2+⋯+??x2+⋯+an??=?xn=c, where at least one of the parameters ?1,?2,…,??a1,a2,…,an is not zero, has a solution in integers if and only if ?⋮???(?1,?2,…,??). Then in this task Natasha can pay ??xi banknotes of the ?i-th nominal value for each ?i, where 1≤?≤?1≤i≤n, and the amount of tax (?1a1?1+?2x1+a2?2+⋯+??x2+⋯+an??xn) can be any number ?c, multiple ?g. (Here some ??<0xi<0, But Natasha can add for each par a sufficiently large number, multiple ?k, that ??xi became greater than zero, the balance from dividing the amount of tax on ?kfrom this will not change.) Therefore, you can replace all pars with one par ?g and the answer from this will not change.

Now we can sort out all the numbers of the form ??mod?gxmodk, where 0≤?<?0≤x<k (further the remainder of the sum, divided by ?k will cycle repeatedly) and output them in ascending order.

Complexity: ?(?+log?+?log?)O(n+log⁡m+klog⁡k), where ?m is the greatest ??ai.

Bonus. Try to improve the complexity to ?(?+?)O(n+k).

 

我的理解:

“has a solution in integers if and only if ?⋮???(?1,?2,…,??). ” 注意这个if and only if !

也就是说

1。 gcd(a1, a2, ..., an) 的倍数必定可以表示为一种线性组合。

2。 其他所有的值,如果不是gcd(a1, a2, ..., an)的倍数,肯定不能由线性组合组成!

所以,把gcd(a1, a2, ..., an)乘一圈%k不就可以得到sum%k的所有值了。。。

#include<bits/stdc++.h>

using namespace std;

int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return (b==0)?a:gcd(b,a%b);
}

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int g=0;
    for(int i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        g=gcd(g,t);
    }
    set<int> ans;
    for(long long i=0,s=0;i<k;i++,s+=g)
        ans.insert(s%k);
    printf("%d\n",ans.size());
    for(set<int>::iterator i=ans.begin();i!=ans.end();i++)
        printf("%d ",*i);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值