hdu 1664 BFS + 数论
来源:互联网 发布:origin软件绘图 编辑:程序博客网 时间:2024/06/08 02:32
这一题主要是要注意一点:
对于任意的整数n,必然存在一个由不多于两个的数来组成的一个倍数。
所以这题只要先用一个数来搜,搜不到就用两个数搜。
这题还有一点要用visit[mod]来叛重,因为是从小到大搜上去的之后出现相同的余数后就算最后搜到了肯定比之前的大。
AC代码如下:
#include <iostream>#include <queue>#include <string>using namespace std;int N;struct {string s;}ans, tempans;int visit[65537];typedef struct{string now;int mod;}Node;int number[2];bool BFS1(){bool flag = false;for( int i = 1; i <= 9; i++ ){Node temp;temp.now = "";temp.now += i + '0';temp.mod = i % N;while( temp.now.size() < N && temp.mod != 0 ){temp.now += i + '0';temp.mod = ( temp.mod * 10 + i ) % N;}if( temp.mod == 0 ){if( !flag ){tempans.s = temp.now;flag = true;}else if( tempans.s.size() > temp.now.size() ){tempans.s = temp.now;}else if( tempans.s.size() == temp.now.size() && tempans.s > temp.now ){tempans.s = temp.now;}}}if( flag ){ans.s = tempans.s;return true;}else{return false;}}void BFS2(){memset( visit, 0, sizeof( visit ) );queue<Node> q;for( int i = 0; i < 2; i++ ){if( number[i] == 0 ){continue;}Node temp;temp.mod = number[i] % N;temp.now = "";temp.now += number[i] + '0';q.push( temp );}bool flag = false;size_t nowmin = 999999;while( !q.empty() ){Node n = q.front();q.pop();if( flag ){if( n.now.size() > tempans.s.size() ){continue;}else if( n.now.size() == tempans.s.size() && n.now > tempans.s ){continue;}if( n.mod == 0 ){tempans.s = n.now;continue;}}else if( n.mod == 0 ){flag = true;tempans.s = n.now;continue;}for( int i = 0; i < 2; i++ ){Node temp = n;temp.mod = ( n.mod * 10 + number[i] ) % N;temp.now += number[i] + '0';if( visit[temp.mod] ){continue;}visit[temp.mod] = 1;q.push( temp );}}if( ans.s == "" ){ans.s = tempans.s;}else if( tempans.s.size() < ans.s.size() ){ans.s = tempans.s;}else if( tempans.s.size() == ans.s.size() && tempans.s < ans.s ){ans.s = tempans.s;}}int main(){while( cin >> N && N ){ans.s = "";if( BFS1() ){ }else{for( int i = 0; i <= 9; i ++ ){for( int j = i + 1; j <= 9; j++ ){number[0] = i;number[1] = j;BFS2();}}}cout << ans.s << endl;}return 0;}
- hdu 1664 BFS + 数论
- hdu 1104 BFS + 数论
- hdu 1104 remainder bfs+数论
- HDU 4294 Multiple (数论+bfs)
- hdu 1104 (数论+bfs)
- hdu 1664 Different Digits (bfs+取余判重+数论知识)
- BFS+余数判重+数论 hdu-1664-Different Digits
- hdu 1664如此爽的一道bfs+数论
- hdu 1104 Remainder(BFS+数论)
- HDU 1495 非常可乐(BFS||数论)
- HDU-1495 非常可乐(BFS)+数论求解
- HDU 1664 Different Digits (数论 -- 鸽笼原理 + BFS搜索 + 余数判重)
- (HDU 1664)Different Digits BFS + 数论 + 余数判重 详细题解
- hdu 1104/poj 2426 Remainder(数论,BFS)
- HDU 1664 BFS
- 1104 Remainder(数论+bfs)
- POJ_3126_Prime Path(数论+BFS)
- hdu1104 Remaindr-----bfs+数论
- 润乾报表——样式的优先级
- 前后台系统VS嵌入式OS,何时该上OS?
- Linux系统如何平滑生效NAT-DNAT改进以及解释
- codeforce 192 div2解题报告
- 与关机相关的几个命令
- hdu 1664 BFS + 数论
- msp430单片机之时钟模块
- mutex与semaphore的区别
- UVaOJ10820 - Send a Table
- 【绿色易维PHP代码加密专家1.3下载】
- 加载第一个rootkit
- hdu1026 Ignatius and the Princess I BFS
- HTML中的相对路径与绝对路径
- ok6410学习笔记(19.块设备驱动程序设计)