1. 程式人生 > >湖南大學第十四屆ACM程式設計大賽 E Easy problem

湖南大學第十四屆ACM程式設計大賽 E Easy problem

連結:https://ac.nowcoder.com/acm/contest/338/E
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
Special Judge, 64bit IO Format: %lld

題目描述

Zghh likes number, but he doesn't like writing problem description. So he will just give you a problem instead of telling a long story for it.
Now given a positive integer x and k digits a1,a2,...,ak, can you find a positive integer y such that y is the multiple of x and in decimal representation y contains all digits of a1,a2,...,ak.

輸入描述:

The first line contains an integer T (1<=T<=10000) which is the number of test case.The following T lines each line is a test case, start with two integer x (1<=x<=1e8) and k (1<=k<=10), k integer a1,a2,..,ak (0<=ai<=9 for i=1..k and ai!=aj for i!=j) is following.

輸出描述:

For each test case output your answer y. Your answer should be a positive integer without leading zero and should be no more than 1e18. Every answer that satisfy the conditions descripted above will be accepted.

示例1

輸入

複製

3
5 3 1 5 7
21 4 2 5 6 9
10 9 0 1 2 3 4 5 6 7 9

輸出

複製

175
2592576
976543210

說明

175 is the multiple of 5 and contains 1,5,7

2592576 is the multiple of 21 and contains 2,5,6,9

976543210 is the multiple of 10 and contains 0,1,2,3,4,5,6,7,9

備註:

Constraint of data

1<=T<=10000

1<=x<=1e8

1<=k<=10

0<=ai<=9,for i=1..k

ai!=aj for i!=j

your answer y should satisfy y <= 1e18

題目大意:

ZGHH喜歡數字,但他不喜歡寫問題描述。所以他只會給你一個問題,而不是長話短說。
現在給一個正整數x和k的數字a1,a2,…,a k,你能找到一個正整數y,這樣y是x的倍數,在十進位制表示中,y包含a1,a2,…,a k的所有數字。
第一行包含一個整數t(1<=t<=10000),它是測試用例的數目。下面的每行t是一個測試用例,從兩個整數x(1<=x<=1e8)和k(1<=k<=10)、k整數a1、a2、、a k(0<=a i<=9,對於i=1..k和a i!AJ為我!=j)如下。
對於每個測試用例,輸出您的答案Y。您的答案應該是不帶前導零的正整數,並且不應超過1E18。所有符合上述條件的答案將被接受。

175是5的倍數,包含1,5,7

2592576是21的倍數,包含2,5,6,9

976543210是10的倍數,包含0、1、2、3、4、5、6、7、9

分析:

找一個數(<=1e18),包含a1,a2,...ak且是x的倍數。(0<=ai<=9,x<=1e8)

仔細讀題會發現題目說所有符合上述條件的答案將被接受。即判題機對程式的結果檢測時只要有一組符合即可,我們可以利用這個特點設定一個通用數n=1234567890,這個數包含ak中的所有個位數。再看x,題目限定(1<=x<=1e8),那麼當n%x==0符合條件時n還是n。當n%x!=0時,要對n進行加工n=n+(x-n%x)以便能夠讓n被x整除,但(1<=x<=1e8)為了不破壞原資料,將n擴大1e8,因為1<=(x-n%x)<=1e8,將待補數放放到1234567890的後面。故初始定義n=123456789000000000。很巧的是這個數也正好在long long 範圍內(比賽時看這個題提交正確的很少,以為是難題,後來才發現理解後這道題真的是easy problem)

#include<iostream>
using namespace std;
int main()
{
	long long n=123456789000000000;//定義初始資料
	int m,x,k,a[100];
	cin>>m;
	while(m--)
	{
		cin>>x>>k;
		for(int i=1;i<=k;i++)//感覺定義初始資料後,輸入這些資料似乎並沒什麼用
			cin>>a[k];
		cout<<n+(x-n%x)<<endl;//對於取模不為0的要補充使其能夠被x整除
	}
}