【GoogleCodeJam2016A】【暴力】Counting Sheep x的倍数从小向大增加直到出现0~9所有数的最小倍增终点

Counting Sheep

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
7 points
Solve A-small
Large input
8 points
Solve A-large

Problem

Bleatrix Trotter the sheep has devised a strategy that helps her fall asleep faster. First, she picks a number N. Then she starts naming N, 2 × N, 3 × N, and so on. Whenever she names a number, she thinks about all of the digits in that number. She keeps track of which digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) she has seen at least once so far as part of any number she has named. Once she has seen each of the ten digits at least once, she will fall asleep.

Bleatrix must start with N and must always name (i + 1) × N directly after i × N. For example, suppose that Bleatrix picks N = 1692. She would count as follows:

  • N = 1692. Now she has seen the digits 1, 2, 6, and 9.
  • 2N = 3384. Now she has seen the digits 1, 2, 3, 4, 6, 8, and 9.
  • 3N = 5076. Now she has seen all ten digits, and falls asleep.

What is the last number that she will name before falling asleep? If she will count forever, print INSOMNIA instead.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a single integer N, the number Bleatrix has chosen.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the last number that Bleatrix will name before falling asleep, according to the rules described in the statement.

Limits

1 ≤ T ≤ 100.

Small dataset

0 ≤ N ≤ 200.

Large dataset

0 ≤ N ≤ 106.

Sample


Input 
 

Output 
 
5
0
1
2
11
1692

Case #1: INSOMNIA
Case #2: 10
Case #3: 90
Case #4: 110
Case #5: 5076


In Case #1, since 2 × 0 = 0, 3 × 0 = 0, and so on, Bleatrix will never see any digit other than 0, and so she will count forever and never fall asleep. Poor sheep!

In Case #2, Bleatrix will name 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. The 0 will be the last digit needed, and so she will fall asleep after 10.

In Case #3, Bleatrix will name 2, 4, 6... and so on. She will not see the digit 9 in any number until 90, at which point she will fall asleep. By that point, she will have already seen the digits 0, 1, 2, 3, 4, 5, 6, 7, and 8, which will have appeared for the first time in the numbers 10, 10, 2, 30, 4, 50, 6, 70, and 8, respectively.

In Case #4, Bleatrix will name 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 and then fall asleep.

Case #5 is the one described in the problem statement. Note that it would only show up in the Large dataset, and not in the Small dataset.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1010, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
void fre() 
{ 
	freopen("c://test//input.in", "r", stdin); 
	freopen("c://test//output.out", "w", stdout); 
}
LL ans[1000010];
void table()
{
	int aim = (1 << 10) - 1;
	for (int X = 1; X <= 1000000; ++X)
	{
		LL x = X;
		int sta = 0;
		int step = 0;
		while (1)
		{
			++step;
			for (LL y = x; y; y /= 10)sta |= 1 << y % 10;
			if (sta == aim)break;
			x += X;
		}
		ans[X] = x;
	}
}
int main()
{
	//fre();
	table();
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		int x;
		scanf("%d", &x);
		if (x == 0)printf("Case #%d: INSOMNIA\n", casei);
		else printf("Case #%d: %lld\n", casei, ans[x]);
	}
	return 0;
}
/*
【题意】
https://codejam.withgoogle.com/codejam/contest/6254486/dashboard
给你一个数x,我们会不停地对x倍增,得到1x,2x,3x,...,nx……
如果在当前所有倍增的数中,已经出现了0~9的所有数,那么我们的倍增就停止。
停止时的数设为y,则我们就要输出y。

【类型】
暴力

【分析】
一个数倍增,总是可以倍增到高位为1,然后各个数字依次出现。
所以我们直接暴力就可以AC啦。

*/


你可能感兴趣的:(暴力,Codejam,题库-google)