[UVA] 12406 - Help Dexter
D |
Help Dexter |
You know Dexter, right? He is a very talented young scientist. He has a huge lab hidden inside his building. He made all possible security arrangement to keep his naughty sister Dee Dee away from his lab. But she always finds a way into the lab. One day Dee Dee came to the lab and started her usual work, messing up Dexter's lab! Dexter was working on a very important project, so he begged to her and said, "Please!!! Not today. I will do anything for you, but please leave this lab today!!!" Dee Dee was waiting for this chance, she said, "Ok, you do my homework I won't disturb you today." What can Dexter do? He agreed. Dee Dee said, "My teacher told me to write down 17 numbers. First one single digit number, second one two digit number, ..., nth one n digit number. They will consist of only digit 1 and 2 and the nth number should be divisible by 2n." Dexter thought, "I have very little time to finish the project. I can't waste my time for this silly problem, I have bigger problem to think!" So, he sent the modified version of this problem to you. Hurry up, Dee Dee is waiting.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with two integers: p q (1 ≤ p, q ≤ 17).
Output
For each case, print the case number first. Then you have to find two integers (smallest and largest) which have p digits and is divisible by 2q. The integers should contain only 1's and 2's. If no result is found, print "impossible". If there is only one integer, then print that integer. Otherwise print both integers (first the smallest one then the largest one) separated by a single space.
Sample Input |
Output for Sample Input |
3 2 2 2 1 2 3 |
Case 1: 12 Case 2: 12 22 Case 3: impossible |
#include <stdio.h>
#include <string.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
long long hp[2][20][20];
void dfs(int idx, long long num) {
int i;
for(i = 1; i <= 17; i++) {
if(num%(1<<i) == 0) {
hp[0][idx][i] = min(hp[0][idx][i], num);
hp[1][idx][i] = max(hp[1][idx][i], num);
} else {
break;
}
}
if(idx == 17)
return;
dfs(idx+1, num*10 + 1);
dfs(idx+1, num*10 + 2);
}
int main() {
memset(hp[0], 127, sizeof(hp[0]));
memset(hp[1], 0, sizeof(hp[1]));
dfs(0, 0);
int t, p, q, c = 0;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &p, &q);
printf("Case %d: ", ++c);
if(hp[0][p][q] == hp[1][p][q])
printf("%lld\n", hp[0][p][q]);
else if(hp[1][p][q] == 0)
puts("impossible");
else
printf("%lld %lld\n", hp[0][p][q], hp[1][p][q]);
}
return 0;
}
love Thank...