Yet Another Multiple Problem 同余定理 bfs

  题意:

给出一个n和m个数

求一个最小的数 1 为n的倍数  2 没有这m个数字中的任意一个

123%n = ((((1%n)*10+2)%n)*10+3)%n

如果     a%n==b%n

那么    (a+x)%n==(b+x)%n

这样就可以剪枝了   之前取模n出现过的后来再出现就可以不要了

例如

A%n==B%n 且 A<B 那么B就不用处理了

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define LL __int64
using namespace std;

const int MAXN = 11111;

struct Node
{
    vector <int> num;
    int c;
} tmp;

queue <Node> q;

int have[MAXN];
int vis[11];

int n;

void bfs()
{
    while(!q.empty()) q.pop();
    memset(have,0,sizeof(have));
    for(int i = 1;i < 10;i++)
        if(!vis[i])
        {
            if(have[i%n]) continue;
            tmp.num.clear();
            tmp.num.push_back(i);
            tmp.c = i%n;
            q.push(tmp);
            have[i%n] = 1;
        }
    while(!q.empty())
    {
        Node cur = q.front();
        if(cur.c == 0) break;
        q.pop();
        for(int i = 0;i < 10;i++)
        {
            if(vis[i]) continue;
            int tt = (cur.c*10+i)%n;
            if(!have[tt])
            {
                have[tt] = 1;
                tmp.num.clear();
                for(int j = 0;j < cur.num.size();j++)
                    tmp.num.push_back(cur.num[j]);
                tmp.num.push_back(i);
                tmp.c = tt;
                q.push(tmp);
            }
        }
    }
    if(!q.empty())
    {
        for(int i = 0;i < q.front().num.size();i++)
            printf("%d",q.front().num[i]);
        puts("");
    }
    else puts("-1");
}

int main()
{
    int m;
    int cas = 0;
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < m;i++)
        {
            int a;
            scanf("%d",&a);
            vis[a] = 1;
        }
        printf("Case %d: ",++cas);
        bfs();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/bxd123/p/10994241.html

时间: 2024-11-13 08:05:28

Yet Another Multiple Problem 同余定理 bfs的相关文章

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

POJ--1465--Multiple【BFS+同余定理】

链接:http://poj.org/problem?id=1465 题意:给一个数字n,和m个数字,找一个由这些数字组成的最小的n的倍数,如果不存在输出0. 思路:这题怎么想都想不到bfs上去,看了别人的解题报告,其实是用bfs来枚举,但是加了一个牛逼的剪枝:同余.即如果A%X==B%X,则(A*10+K)%X==(B*10+K)%X. 我们枚举m中每一个数字做这个K,实际上是枚举了一个数,B*10是之前枚举的数字,如果这个数%X得到的值之前已经得到过,则没必要再往下计算,因为根据同余定理剩下的

Yet Another Multiple Problem(bfs好题)

Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 1 Problem Description There are tons of problems about integer multiples. Despite the f

HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3407    Accepted Submission(s): 825 Problem Description There are tons of problems about integer multiples. Despit

hdu 1664 Different Digits, spoj 3929 , 同余,bfs

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 202 Problem Description Given a positive integer n, your task is to find a positive integer m, which is a multiple

HDOJ 4474 Yet Another Multiple Problem

BFS..... Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3307    Accepted Submission(s): 806 Problem Description There are tons of problems about integer multiple

Light oj 1214-Large Division (同余定理)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我就把A的每一位拆开,比如A是2341,那么2341=2000+300+40+1,然后你懂的... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

HDU 4474 Yet Another Multiple Problem

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4734    Accepted Submission(s): 1021 Problem Description There are tons of problems about integer multiples. Despit

NYOJ 420 p次方求和 (快速幂+同余定理)

题目描述: http://acm.nyist.net/JudgeOnline/problem.php?pid=420 一个很简单的问题,求1^p+2^p+3^p+--+n^p的和. 输入 第一行单独一个数字t表示测试数据组数.接下来会有t行数字,每行包括两个数字n,p, 输入保证0<n<=1000,0<=p<=1000. 输出 输出1^p+2^p+3^p+--+n^p对10003取余的结果,每个结果单独占一行. 样例输入 210 110 2 样例输出 55385 题目分析: 快速幂