1495: Help Dexter
时间限制: 1 Sec 内存限制: 128 MB
提交: 150 解决: 48
[提交][状态][讨论版]
题目描述
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 2^n." 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 starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with two integers: p q (1 ≤ p, q ≤ 17).
输出
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 2^q. The integers should contain only 1 and 2. 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.
样例输入
3
2 2
2 1
2 3
样例输出
Case 1: 12
Case 2: 12 22
Case 3: impossible
分两条路径搜:dfs(ans*10+1,k+1),dfs(ans*10+2,k+1),注意整数溢出
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=2222222222222222;
LL a[2000000];
int key,p,j;
void dfs(LL ans,int k)
{
if(k>p) return;
if(ans>0&&ans%key==0&&k==p) a[j++]=ans;
dfs((LL)ans*10+1,k+1);
dfs((LL)ans*10+2,k+1);
}
int main()
{
int t,q,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&q);
printf("Case %d: ",cas++);
key=(1<<q);
j=0;
dfs(0,0);
if(!j)
printf("impossible\n");
else if(j==1)
printf("%lld\n",a[0]);
else
{
sort(a,a+j);
printf("%lld %lld\n",a[0],a[j-1]);
}
}
}