Help Dexter(DFS)

题目描述
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,q,1<=p,q<=17;问是否存在p位只由1和2组成的数字能够整除2^q;若不存在,输出“impossible”;存在的话,分别求出能整除它的p位数的最小和最大值,如果无最大值,就只输出最小值。

思路:DFS 首先想到了从小到大和从大到小分别搜索一遍,但是时间超限了。 然后有个想法,打表,但是运行到后面就很慢;看到一个学长写的打表,前面的一部分都可以打表得到,但是后面的就太慢,得不到了。

然后,换了一种写法,把之前用字符表示的改为数字,因为17位数在long long 的范围内,故可以用long long 储存结果;每一位都有两种取法1和2;所以有2^17种,用cnt记录当前数字的位数,等于p时,就判断是否整除,再返回上一级调用

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
const LL INF=3e17;
int p,q,cnt=0;
LL n,num,MAX,MIN;
void dfs()
{
    if(cnt==p)///cnt是数字num当前的位数,如果cnt等于题目所给p
    {
        if(num%n==0)
        {
            if(num<MIN) MIN=num;//求出的第一个可以整除的数就一定是最小的
            if(num>MAX) MAX=num;
            //printf("%lld %lld\n",MIN,MAX);
        }
        return ;
    }
    for(int i=1; i<=2; i++)
    {
        cnt++;
        num=num*10+i;
        //printf("%d  %lld\n",cnt,num);
        dfs();
        cnt--;
        num=(num-i)/10;
    }
    return ;
}
int main()
{
    int t,k=0;
    scanf("%d",&t);
    while(t--)
    {
        num=cnt=0;
        n=1;
        scanf("%d%d",&p,&q);
        MAX=0,MIN=INF;
        for(int i=0; i<q; i++)
            n*=2;
        dfs();
        //printf("%lld    %lld\n",MIN,MAX);
        printf("Case %d: ",++k);
        if(MIN==INF)
            printf("impossible\n");
        else if(MAX==MIN) printf("%lld\n",MIN);
        else printf("%lld %lld\n",MIN,MAX);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值