CodeForces 248B Chilly Willy

题目描述

Description

Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.

Chilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.

A number’s length is the number of digits in its decimal representation without leading zeros.

Input

A single input line contains a single integer n (1 ≤ n ≤ 10^5).

Output

Print a single integer — the answer to the problem without leading zeroes, or “-1” (without the quotes), if the number that meet the problem condition does not exist.

Sample test(s)

input
1
output
-1
input
5
output
10080

题目链接

http://codeforces.com/problemset/problem/248/B

解题思路

题目大意:给一个n,求一个最小的n位数,使这个数能同时被2,3,5,7整除。
解题思路:由于我们要找的数是能被210整除的,所以可以得出来我们求的数一定是一个位数为3位循环的数,因为要保证最小的数,因为10 ^ n % a = x,所以我们找数的时候只需找上10 ^ n + y(此处的y就是循环节,也就是210 - x)这个数就好了。

AC代码

0 KB/30 ms/GNU G++ 4.9.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>

int num[] = {50, 80, 170, 20, 200, 110};
int main()
{
int n;
while (~scanf("%d", &n))
{
if (n < 3) printf("-1\n");
else if (n == 3) printf("210\n");
else
{
printf("1");
for (int i = 0; i < n - 4; i++)
{
printf("0");
}
printf("%03d\n", num[(n - 4) % 6]);
}
}
return 0;
}