Codeforces Round #499 (Div. 2) E Border

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 (000" role="presentation" style="display: inline-block; line-height: 0; font-size: 16.38px; word-wrap: normal; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; margin: 0px; padding: 1px 0px; position: relative;">1n100000000" role="presentation" style="display: inline-block; line-height: 0; font-size: 16.38px; word-wrap: normal; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; margin: 0px; padding: 1px 0px; position: relative;">2k100000) — the number of denominations of banknotes and the base of the number system on Mars.

The second line contains n integers a1,a2,,an (1ai109) — 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.

 

 题解:

 

 裴蜀定理板题,然而当时玩交互题去了,结果最后没时间打了......

 

先求出所有数模k后的gcd,由裴蜀定理,(gcd*i)%k(i=1,2,3,......k)必然是其中一个答案,枚举i即可

 

注意对0的处理

 

#include<bits/stdc++.h>
using namespace std;
int n,k,ans,gcd=-1,num;
bool mk=false;
bool vis[100005];
int get_gcd(int a,int b)
{   
    return b==0?a:get_gcd(b,a%b);
}
int main()
{
    memset(vis,false,sizeof(vis));
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        {
            scanf("%d",&num);
            num=num%k;
            if(!num)
               mk=true;
            if(num&&gcd!=-1)
               gcd=get_gcd(gcd,num);
            if(num&&gcd==-1)
               gcd=num;      
          }
    if(gcd==-1)
       printf("1\n0");
    else
       {
              int tot=0;
              if(mk)
                 {
                      tot++;
                    vis[0]=true;
                 }
              for(int i=1;i<=k;i++)
               if(!vis[(i*(long long)gcd)%(long long)k])
                  {
                        tot++;
                      vis[(i*(long long)gcd)%(long long)k]=true; 
                  }
           printf("%d\n",tot);
           for(int i=0;i<k;i++)
               if(vis[i])
                  printf("%d ",i);          
       }             
    return 0;
}
View Code

 

 

posted @ 2018-07-29 16:39  nanjoln0  阅读(185)  评论(0编辑  收藏  举报