hnust

来源:互联网 发布:阿里云备案需要多久 编辑:程序博客网 时间:2024/04/28 20:05

题目描述

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.

样例输入

3
2 2
2 1
2 3

样例输出

Case 1: 12
Case 2: 12 22
Case 3: impossible

题解

  • 题意是求出所有p位整数数且只由1和2组成的、能被2^q整除数中的最大值和最小值。
  • p可以到17位,定义类型试用long long型。
  • 显然用dfs很快。具体如下。
#include <cstdio>#include <cstring>using namespace std;typedef long long ll;const ll INF=1e18;int p,q;ll minn,maxn,num=0;int er[20];void dfs(int stp){    if(stp==p){        if(num%er[q]==0){            if(num<minn) minn=num;            if(num>maxn) maxn=num;        }    }else{        for(int i=1;i<=2;i++){            num=num*10+i;            dfs(stp+1);            num=(num-i)/10;        }    }}int main(){    int T;    er[0]=1;    for(int i=1;i<=17;i++)        er[i]=er[i-1]*2;    scanf("%d",&T);    for(int cse=1;cse<=T;cse++)    {        scanf("%d%d",&p,&q);        maxn=0;minn=INF;        dfs(0);        printf("Case %d: ",cse);        if(minn==INF&&maxn==0) printf("impossible\n");        else if(minn==maxn) printf("%lld\n",minn);        else printf("%lld %lld\n",minn,maxn);    }    return 0;}
原创粉丝点击