HDU 4474&&POJ 1465 BFS&&余数判重

两道题只是输入输出格式略有不同

给出n,m,m个数

要求生成一个数x,是n的倍数,并且只由这M个数构成(或不能含有这M个数中的任意一个),求最小的x,没有则输出-1(0);

BFS找每一个满足n的倍数的数,用余数判重优化

对于给定两个数A,B,如果A和B模N都相同,设为C,即:

A=x*N+C

B=y*N+C

那么如果在A后面加上一个数字能够被N整除,则在B后面加上这个数字也肯定可以被N整除

即:(A*10+X)%N==(B*10+X)%N

因此可以利用模N的结果进行判重,只保留相同余数的最小数,于是采用BFS搜索

HDU4474:

#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;

int hash[11],num[10010],pre[10010];
int n;

void pri(int k)
{
    if (pre[k]!=-1) pri(pre[k]);
    printf("%d",num[k]);
}
void bfs()
{
    queue<int>q;
    int i,cur,next;

    for (i=1;i<=9;i++)
        if(hash[i]==0)
        {
            cur=i%n;
            if (cur==0) { printf("%d\n",i); return ;}
            num[cur]=i;
            q.push(cur);
        }

    while (!q.empty())
    {
        cur=q.front();
        q.pop();
        for (i=0;i<=9;i++)
            if(hash[i]==0)
            {
                next=(cur*10+i)%n;
                if (num[next]==-1)
                {
                    q.push(next);
                    num[next]=i;
                    pre[next]=cur;
                }
                if (next==0) {pri(next); printf("\n"); return ;}
            }
    }
    printf("-1\n");
    return ;
}
int main()
{
    int m,x,Case=1;
    while (scanf("%d",&n)!=EOF)
    {
        memset(hash,0,sizeof(hash));
        memset(num,-1,sizeof(num));
        memset(pre,-1,sizeof(pre));

        scanf("%d",&m);
        while (m--)
        {
            scanf("%d",&x);
            hash[x]=1;
        }
        printf("Case %d: ",Case++);
        if(n==0) { printf("-1\n"); continue;}
        bfs();
    }
    return 0;
}

POJ1465:

#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;

int hash[11],num[10010],pre[10010];
int n;

void pri(int k)
{
    if (pre[k]!=-1) pri(pre[k]);
    printf("%d",num[k]);
}
void bfs()
{
    queue<int>q;
    int i,cur,next;

    for (i=1;i<=9;i++)
        if(hash[i]==1)
        {
            cur=i%n;
            if (cur==0) { printf("%d\n",i); return ;}
            num[cur]=i;
            q.push(cur);
        }

    while (!q.empty())
    {
        cur=q.front();
        q.pop();
        for (i=0;i<=9;i++)
            if(hash[i]==1)
            {
                next=(cur*10+i)%n;
                if (num[next]==-1)
                {
                    q.push(next);
                    num[next]=i;
                    pre[next]=cur;
                }
                if (next==0) {pri(next); printf("\n"); return ;}
            }
    }
    printf("0\n");
    return ;
}
int main()
{
    int m,x,Case=1;
    while (scanf("%d",&n)!=EOF)
    {
        memset(hash,0,sizeof(hash));
        memset(num,-1,sizeof(num));
        memset(pre,-1,sizeof(pre));

        scanf("%d",&m);
        while (m--)
        {
            scanf("%d",&x);
            hash[x]=1;
        }
      //  printf("Case %d: ",Case++);
        if(n==0) { printf("0\n"); continue;}
        bfs();
    }
    return 0;
}
时间: 2024-08-08 05:35:23

HDU 4474&&POJ 1465 BFS&&余数判重的相关文章

[bfs+余数判重+路径记录] hdu 4474 Yet Another Multiple Problem

题意: 给一个n和m个数字(一位数) 求最小的n的倍数不含有这m个数字,不存在输出-1 思路: 首先有可能这个数超long long 所以无法暴力解决 所以这题应该是一个bfs 为什么能用余数判重呢 对于当前的余数进到队列里,一定是这个余数对应数的最小值 接下来再怎么添加到满足条件的后续东西应该是一样的 所以就可以余数判重了,类似数位dp的记录方式 然后再加上一个路径记录就好了 代码: #include"cstdlib" #include"cstdio" #incl

poj1465Multiple(经典BFS+余数判重)

Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6936   Accepted: 1495 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the small

HDU 2579 Dating with girls(2) BFS 余数判重

对于石头的处理就按照每个位置的时间取k的余数判一下重复就好,其他随意写 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include

11198 - Dancing Digits(BFS + hash判重)

题目:11198 - Dancing Digits 题目大意:每组数据给出8个数字,可能正可能负.要求最后将这8个数字按照数字绝对值从小到大的排序.排序的规则是让某个数字a邀请另一个数字b跳舞,这样a就可以插到b的左边或是右边,a能邀请b跳舞,则a* b <0 ,且a+b要是素数.题目问给出一组数据问能否通过邀请跳舞来排序,能的话就输出最少的邀请次数,否则输出-1. 解题思路:这题一开始竟然想着dfs,但是后面发现,这样的判断树可以是无限大,因为可以a邀请完b,然后b在邀请a,这样一来一回有可能

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

Keyboarding (bfs+预处理+判重优化)

# #10030. 「一本通 1.4 练习 2」Keyboarding [题目描述] 给定一个 $r$ 行 $c$ 列的在电视上的"虚拟键盘",通过「上,下,左,右,选择」共 $5$ 个控制键,你可以移动电视屏幕上的光标来打印文本.一开始,光标在键盘的左上角,每次按方向键,光标总是跳到下一个在该方向上与当前位置不同的字符,若不存在则不移动.每次按选择键,则将光标所在位置的字符打印出来. 现在求打印给定文本(要在结尾打印换行符)的最少按键次数. [算法] 1.预处理四个方向能到达的点.

Hdu2437-Jerboas(取余数判重搜索)

Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long hind legs. Jerboas shelter in well-hidden burrows. They create two types of burrow: temporary and permanent. The temporary burrows are plain tubes whil

hdu 1226 超级密码 BFS 挺不错的题啊!

超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2883    Accepted Submission(s): 928 Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进制的

poj 1465 &amp; zoj 1136 Multiple (BFS+余数重判)

Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6177   Accepted: 1346 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the small