Codeforces Global Round 7
题干
You are given a integer n (n>0). Find any integer s which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of s:
s>0,
s consists of n digits,
no digit in s equals 0,
s is not divisible by any of it’s digits.
Input
The input consists of multiple test cases. The first line of the input contains a single integer t (1≤t≤400), the number of test cases. The next t lines each describe a test case.
Each test case contains one positive integer n (1≤n≤105).
It is guaranteed that the sum of n for all test cases does not exceed 105.
Output
For each test case, print an integer s which satisfies the conditions described above, or “-1” (without quotes), if no such number exists. If there are multiple possible solutions for s, print any solution.
Example
input
4
1
2
3
4
output
-1
57
239
6789
Note
In the first test case, there are no possible solutions for s consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: 23, 27, 29, 34, 37, 38, 43, 46, 47, 49, 53, 54, 56, 57, 58, 59, 67, 68, 69, 73, 74, 76, 78, 79, 83, 86, 87, 89, 94, 97, and 98.
For the third test case, one possible solution is 239 because 239 is not divisible by 2, 3 or 9 and has three digits (none of which equals zero).
解题思路
我们先来考虑哪些数字是不能出现在s中的。
1,任何数字都能被1整除,排除
2,不能出现在数字的个位,因为2为个位数一定能被2整除
5,不能出现在数字的个位,理由同上
那么有没有一个数字非常好判断一个整数能不能被它整除呢?
有,那就是5。
只需看个位数字是不是0或5就行了,又因为s本身不能包含0,我们只需要保证个位数字不是5就能保证这个数字不被5整除。
那么最后一个数字怎么保证?
我们分类讨论,当(n-1)%3 != 0时,我们把个位设置为3
这个很容易理解,前n-1位数字之和为5*(n-1),个位数字是3,当(n-1)%3 != 0时,5*(n-1)也不会被3整除
当(n-1)%3 == 0 时,我们把后两位设置为43
因为此时数字55555…53是会被3整除的
我们把最后两位数字设置为43,相当于减去10,自然就不再会被3整除
同时555555…00是100的倍数,被4整除
而43不被4整除,所以总体不会被4整除,构造完毕
#include <iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
if(n==1){
cout<<-1<<endl;
continue;
}
if((n-1)%3){
for(int i=0;i<n-1;i++)cout<<"5";
cout<<'3'<<endl;
}
else{
for(int i=0;i<n-2;i++)cout<<"5";
cout<<"43"<<endl;
}
}
}