uva1653
本来想刷道签到题结果被卡住了。这题题意描述有点问题,数字又不一定都是个位数。。。难道是我英语太差了? digits就表示0~9这几个数?唉,还是太弱了。这题就是用了一个bfs,应该说还是有点意思的,直接模拟复杂度肯定爆炸,而且还不好判无解的情况。而bfs复杂度是n*10的,因为%n相同的状态搜索过程都是相同的,相当于一堆循环节,而无解的情况也好判了,状态都搜完后还没有%n为0的就无解了,因为剩下的都是一堆循环节,也不可能搜到解了。
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct node{
int m,f,x;
}q[maxn],A,B;
int cas,n,m,can[],head,tail,vis[maxn],flag;
void print(int x){
if(!x)return;
print(q[x].f);
printf("%d",q[x].x);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
++cas;flag=;int x;
memset(vis,,sizeof(vis));
memset(can,,sizeof(can));
for(int i=;i<=m;++i){
scanf("%d",&x);can[x]=;
}
printf("Case %d: ",cas);
head=tail=;
for(int i=;i<=;++i)if(!can[i]){
A.m=i%n;A.x=i;A.f=;
if(!A.m){printf("%d\n",i);flag=;break;}
if(!vis[A.m]){
q[++tail]=A;vis[A.m]=;
}
}
if(flag)continue;
while(head<tail){
B=q[++head];
for(int i=;i<=;++i)if(!can[i]){
A.m=(B.m*+i)%n;
A.x=i;A.f=head;
if(vis[A.m])continue;
vis[A.m]=;q[++tail]=A;
if(!A.m){flag=;break;}
}
if(flag)break;
}
if(flag){
print(tail);printf("\n");
}
else{puts("-1");}
}
//system("pause");
return ;
}
/*
2345 3
7 8 9
100 1
0
*/
uva1653的更多相关文章
随机推荐
- vue 获取组件 和 dom 对象 ref/el
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ES6 Symbol的应用场景
一.简介 具体使用请参考:API ES6 引入了一种新的原始数据类型Symbol,表示独一无二的值.它是 JavaScript 语言的第七种数据类型,前六种是:undefined.null.布尔值(B ...
- [转载]RPM中SPEC常用路径以及宏变量
转自:http://blog.csdn.net/txgc1009/article/details/6833764 通过命令rpm --showrc查看实现代码.另外直接通过 rpm --eval &q ...
- SQL Server 2008设置sa用户并开启远程连接
1.打开SQL Server Management Studio,以windows身份登录数据库
- [SoapUI] Groovy获取HTTP Status
def value = messageExchange.responseHeaders["#status#"] def httpResponseHeaders = context. ...
- 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)
我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclips ...
- mysql8.0.4以后修改密码方式变更
https://blog.csdn.net/qq_38265784/article/details/80915098 use mysql: ALTER USER 'root'@'localhost' ...
- Scrapy的安装和基本使用方法
Scrapy的安装 1. Windows下安装流程: 方法一: 命令行执行pip install scrapy 安装scrapy 注意:如果有anaconda,也可以打开“Anaconda promp ...
- PHP可变函数
可变函数是指如果一个变量名后有圆括号,PHP将寻找与变量的值同名的函数,并尝试执行它 可变函数可以用来实现包括回调函数,函数表在内的用途 $str = 'strtolower'; echo $str( ...
- css过渡动画
具体代码:1.水平翻转-moz-transform:scale(-1,1);-webkit-transform:scale(-1,1);-o-transform:scale(-1,1);transfo ...