hdu 4474数位搜索

换种思考角度

在符合数字排列组合的情况下,观察数字是否为n的倍数。

于是从小到大查找,通过bfs状态转换。

循环节为n。表达困难,还请看代码

#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include<cstdlib>
#include<math.h>
#include<iostream>
#include<limits.h>
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

using namespace std;

int n,m;
int a[10];
int num[10005];//记录每位余数的真实数;
int ans[10005];
int pre[10005];
int c = 0;
void init()
{
    clr(a,0);
    clr(num,-1);
    clr(pre,-1);
}

void pri(int t)
{
    int c = 0;
    while(pre[t]!=-1)
    {
        ans[c++] = num[t];
        t = pre[t];
    }
    ans[c] = num[t];
    for(int j = c; j >= 0; j--)
        printf("%d",ans[j]);
}

void bfs()
{
    int t;
    queue <int> q;
    for(int i = 1; i < 10; i++)//queue预装1-9
    {
        if(!a[i])
        {
            t = i%n;
            if(t==0){printf("%d",i);return;}
            if(num[t]==-1)
            {
                q.push(t);num[t] = i;
            }
        }
    }

    //按位由小到大搜索
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int i =0;i<10; i++)
        {
            if(!a[i])
            {
                t = (u*10+i)%n;
                if(num[t]==-1)
                {
                    num[t] = i;
                    pre[t] = u;
                    q.push(t);
                }
                if(t==0)
                {
                    pri(t);
                    return;
                }

            }
        }
    }

    printf("-1");
}

int main()
{
    int kase = 1;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        int x;
        for(int i =1; i <= m; i++)
        {
            scanf("%d",&x);
            a[x] = 1;
        }
        printf("Case %d: ",kase++);
        bfs();
        printf("\n");
    }
}

时间: 2024-10-12 22:50:30

hdu 4474数位搜索的相关文章

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

HDU 4474(神奇的BFS+强剪枝)

 HDU - 4474 Yet Another Multiple Problem Time Limit: 20000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is hi

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

HDU 2828 DLX搜索

Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 771    Accepted Submission(s): 230 Special Judge Problem Description There are several switches and lamps in the room, however, the connecti

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

HDU 1518 Square 搜索

Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 11   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a set of sticks of vario

hdu 5787 数位dp,记忆化搜索

题意:求区间[l,r]内有多少个数符合,这个数的任意的相邻k位数(digits),这k个数都两两不相等 l,r范围是1~1e18,k是2~5 思路:数位DP,因为K<=5,我们最多需要保存下来当前位的前4位就足够了.因为dp[pos][p1][p2][p3][p4]表示,现在枚举取第pos位,pos位之前的四位分别为p1,p2,p3,p4,p4是pos的上一位.那么p1~p4的范围就是0~9,但是因为总位数小于当前数字的位数的数也要进行枚举,需要一个数字来区分它是前导0还是在中间时为0,令p =