hdu1664 Different Digits (搜索)
来源:互联网 发布:美国网络中立 编辑:程序博客网 时间:2024/06/05 07:11
Different Digits
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 734 Accepted Submission(s): 173
Problem Description
Given a positive integer n, your task is to find a positive integer m, which is a multiple of n, and that m contains the least number of different digits when represented in decimal. For example, number 1334 contains three different digits 1, 3 and 4.
Input
The input consists of no more than 50 test cases. Each test case has only one line, which contains a positive integer n ( 1<=n < 65536). There are no blank lines between cases. A line with a single `0' terminates the input.
Output
For each test case, you should output one line, which contains m. If there are several possible results, you should output the smallest one. Do not output blank lines between cases.
Sample Input
7 15 16 101 0
Sample Output
7555161111
Source
2004 Asia Regional Shanghai
Recommend
xhd
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<algorithm>//#pragma comment (linker,"/STACK:1024000000000,102400000000")#define INF 0x3f3f3f3f#define MAXN 66666using namespace std;int n,curcnt,anscnt,da[11];bool done[MAXN];string ans,curans;struct node{ int d,yu,pre,cnt; node(int dd=0,int vv=0,int pp=0,int cc=0) { d=dd;yu=vv;pre=pp;cnt=cc; }}q[MAXN];bool dfs(int k){ int st=0,en=0; memset(done,0,sizeof done); q[en++]=node(0,0,-1,0); while(st<en) { node x=q[st++]; if(x.cnt==anscnt)return 0; for(int i=0;i<k;i++) { if(x.yu==0&&da[i]==0)continue; int curyu=(x.yu*10+da[i])%n; if(done[curyu])continue; if(curyu==0) { curans=""; curcnt=x.cnt+1; curans+=da[i]+'0'; int ii=st-1; while(q[ii].pre!=-1) { curans+=q[ii].d+'0'; ii=q[ii].pre; } reverse(curans.begin(),curans.end()); return 1; } done[curyu]=1; q[en++]=node(da[i],curyu,st-1,x.cnt+1); } } return 0;}int main(){ int i,j; while(~scanf("%d",&n)&&n) { anscnt=70000; for(da[0]=1;da[0]<=9;da[0]++) if(dfs(1)) { if(anscnt>curcnt) { ans=curans; anscnt=curcnt; } } if(anscnt==70000) for(da[0]=0;da[0]<=9;da[0]++) for(da[1]=da[0]+1;da[1]<=9;da[1]++) if(dfs(2)) { if(anscnt>curcnt) { ans=curans; anscnt=curcnt; } else if(anscnt==curcnt&&curans<ans) { ans=curans; } } cout<<ans<<endl; } return 0;}
0 0
- hdu1664 Different Digits (搜索)
- hdu1664 Different Digits (搜索)
- HDU1664 Different Digits(bfs +数论剪枝)
- hdu1664 Different Digits
- 【搜索】 HDU 1664 Different Digits
- 【hdu】1664 different digits【搜索+字符串处理】
- HDU 1664 Different Digits (数论 -- 鸽笼原理 + BFS搜索 + 余数判重)
- UVA - 1341 Different Digits (同余)
- hdu 1664 Different Digits
- HDU 1664 Different Digits
- HDU 1664 Different Digits
- POJ 2283 Different Digits
- hdu Different Digits 1664
- hdu 1664 Different Digits
- UVa 12527 - Different Digits
- HDOJ 1664 Different digits
- hdu 1664 Different Digits (bfs+取余判重+数论知识)
- hdu 1664 Different Digits(bfs+余数判重)
- JFreeChart C/S和B/S简单例子
- 新人一枚
- hdoj 3507 dp四边形不等式优化(其实用斜率dp更优)
- Cocos2dx3.1 获取当前系统时间
- 应用程序服务器的错误
- hdu1664 Different Digits (搜索)
- android 内存溢出问题分析
- HDU-2089 不要62
- 优秀有价值的博客收藏列表(持续更新)
- 卷积特征提取与池化(Pooling)——处理大型图像
- php + mysql 分布式事务(xa)
- hdu 1200 To and Fro
- JAVA:Stack栈和Heap堆的区别
- NYOJ44,子串和