题目描述
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 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 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
题意如上述
选择了BFS方法。
剪枝选择了对搜出的结果n,若n的后x位可以被2的x次方整除,则n也可以被整除这样的思想。
选择了BFS方法。
剪枝选择了对搜出的结果n,若n的后x位可以被2的x次方整除,则n也可以被整除这样的思想。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int p,q,End,pan;
typedef char CC[18];
CC CH[100000],CH2[100000];
void INIT_min()
{
for(int i= 1; i < p; i++)
CH[1][i]='1';
CH[1][p]='2';
}
void INIT_max()
{
for(int i= 1; i <= p; i++)
CH2[1][i]='2';
}
int panduan(CC fi)
{
long long ans=0;
for(int i=1; i<=p; i++)
ans=ans*10+(fi[i]-'0');
//cout<<ans<<endl;;
if(ans%End==0)
return 1;
return 0;
}
int jian(CC fi,int a)
{
long long ans=0,x;
for(int i=p-a;i<=p;i++)
ans=ans*10+(fi[i]-'0');
x=pow(2,a);
if(ans%x==0)
return 1;
return 0;
}
int BFS_min()
{
int R=2,F=1,i,j;
while(F < R)
{
CC& fi=CH[F];
if(panduan(fi))
return F;
// F++;
for(i = 1; i<p; i++)
{
CC& se=CH[R];
for(j = 1; j<= p; j++) se[j]=fi[j];
if(se[i]=='1')
{
se[i]='2';
if(jian(se,p-i)==1)
R++;
}
}
F++;
// cout<<R<<endl;
}
return-1;
}
int BFS_max()
{
int R=2,F=1,i,j;
while(F < R)
{
CC& fi=CH2[F];
//cout<<fi[1]<<fi[2];
if(panduan(fi)==1)
return F;
//F++;
for(i = 1; i<p; i++)
{
CC& se=CH2[R];
for(j = 1; j<= p; j++) se[j]=fi[j];
if(se[i]=='2')
{
se[i]='1';
if(jian(se,p-i)==1)
R++;
}
}
F++;
}
return-1;
}
int main()
{
int T,i,j,res_min,res_max;
scanf("%d",&T);
for(i = 1; i<= T; i++)
{
scanf("%d %d",&p,&q);
End=pow(2,q);
INIT_min();
res_min= BFS_min();//cout<<res_min;
INIT_max();
res_max=BFS_max();//cout<<res_max;
printf("Case %d: ",i);
if(res_min==-1)
printf("impossible\n");
else if(memcmp(CH2[res_max],CH[res_min],sizeof(CC))==0)
{
for(j = 1; j <=p; j++)
printf("%c",CH[res_min][j]);
printf("\n");
}
else
{
for(j = 1; j <=p; j++)
printf("%c",CH[res_min][j]);
printf(" ");
for(j = 1; j <=p; j++)
printf("%c",CH2[res_max][j]);
printf("\n");
}
}
}