B. Chilly Willy
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ≤ 100000).
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.
Examples
input
1
output
-1
input
5
output
10080
2、3、5、7的最小公倍数为210
当1<=n<=2时输出-1
当n=3时输出210
当n>=4时:给定任意一个大于最小值的n位数,位数不变的情况下若不是最小值就依次递减210,我们会得到首位为1,因为为210的倍数,末尾为0,末二三位未知,其余位为零的数
首先n=4输出1050,当运算n=5时对1050*10,按上述方法递减210,得到10080依照此规律向下寻找n=6…
因为3的倍数是有规律的,各位数字加和为3的倍数,所以未知的两位有有限个,上面递推过程一旦有一个重复便开始循环。先通过暴力加和的方法找出n=4~10 的结果,结果如下:
n=4 1050
n=5 10080
n=6 100170
n=7 1000020
n=8 10000200
n=9 100000110
n=10 1000000050
所以末三位为050、080、170、020、200、110的循环
#include<iostream>
using namespace std;
int main()
{
int n, n1;
cin >> n;
if (n <= 2) cout << -1;
else if (n == 3) cout << 210;
else {
cout << 1;
n1 = n - 4;
for (int i = 0; i < n1; i++) cout << 0;
if ((n - 3) % 6 == 1) cout << "050";
else if ((n - 3) % 6 == 2) cout << "080";
else if ((n - 3) % 6 == 3) cout << "170";
else if ((n - 3) % 6 == 4) cout << "020";
else if ((n - 3) % 6 == 5) cout << "200";
else if ((n - 3) % 6 == 0) cout << "110";
}
}