E. Border Codeforces Round #499 (Div. 2)

本文介绍了宇航员Natasha在火星上遇到的税收问题,需要了解火星货币的面额和当地使用的k进制。Natasha需要找出能够使火星居民满意的神圣数字(即税款在k进制下最后一位)。通过裴蜀定理计算所有可能的数字组合,找出符合条件的神圣数字数量及其具体值。

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 (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.

题意:给你n个数,每个数能无限次和其他任意个数自由组合得到一个新的数,所有新的数在k进制下的最后一位有多少种情况,按升序输出所有情况。

思路:预处理出每一个数在k进制下的最后一位的情况,然后得出他们的最大公因数,根据裴蜀定理 得到的最大公因数可以算出他们的在k内的所有可能组合,注意超过的部分要取模。

#include<bits/stdc++.h>
using namespace std;
const int maxn = int(1e5) + 100;
int num[maxn];
int tmp[maxn];
bool vis[maxn];
int gcd(int a,int b) {
	return b==0?a:gcd(b,a%b);
}
void solve(int n,int k) {
	for(int i=1; i<=n; i++)num[i]%=k;
	int comgcd=gcd(num[1],num[2]);
	for(int i=3; i<=n; i++)
		comgcd=gcd(num[i],comgcd);
	sort(num+1,num+n+1);
	memset(vis,0,sizeof(vis)); 
	for(int i=1; i<=n; i++) {
		for(int j=num[i]; vis[j]==0; j=(j+comgcd)%k)
			vis[j]=1;
	}
	int top=0;
	for(int i=0; i<k; i++)
		if(vis[i]) tmp[top++]=i;
	printf("%d\n",top);
	for(int i=0; i<top; i++)
		printf("%d%c",tmp[i],i==top-1?'\n':' ');
}
int main() {
	int n,k;
	while(~scanf("%d %d",&n,&k)) {
		for(int i=1; i<=n; i++)
			scanf("%d",&num[i]);
		solve(n,k);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值