Different Digits

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1430    Accepted Submission(s): 392

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
7
555
16
1111
 
Source
 

一个数论中的结论:对于任意的整数n,必然存在一个由不多于两个的数来组成的一个倍数。因为a,aa,aaa……取n+1个,则必有两个模n余数相同,相减即得n的倍数m。而m只由a、0组成。

有了上述结论+同余剪枝,这个题就能够做出来了,还有这题需要手动记录路径,不能让string 在结构体里面,不然TLE,记录路径的话,由于实在是找不到独一无二的标志,所以将每个节点作为独一无二的标志,手动模拟队列记录。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
using namespace std;
#define N 65536
typedef long long LL;
int n;
bool mod[N];
struct Node{
int val,step,mod,pre;
}q[N];
int a[];
int bfs(int k){
memset(mod,,sizeof(mod));
int head = ,tail = -;
for(int i=;i<=k;i++){
if(a[i]!=){
Node s;
s.val = a[i],s.step = ;
s.pre = -,s.mod = a[i]%n;
mod[s.mod] = true;
q[++tail] = s;
}
}
while(head<=tail){
Node now = q[head];
if(now.mod==){
return head;
}
for(int i=;i<=k;i++){
Node next;
next.mod = (now.mod*+a[i])%n;
next.pre = head;
next.val = a[i];
next.step = now.step+;
if(!mod[next.mod]){
mod[next.mod] = true;
q[++tail] = next;
}
}
head++;
}
return -;
}
string ans,res;
void getans(int k){
if(k==-) return ;
getans(q[k].pre);
ans+=(q[k].val+'');
}
int main(){
while(scanf("%d",&n)!=EOF,n){
res = "";
for(int i=;i<=;i++){
a[] = i;
int f = bfs();
if(f!=-){
ans = "";
getans(f);
if(res.compare("")==) res = ans;
else if(res.length()>ans.length()) res = ans;
}
}
if(res.compare("")!=){
cout<<res<<endl;
continue;
}
for(int i=;i<=;i++){
a[] = i;
for(int j=i+;j<=;j++){
a[] = j;
int f = bfs();
if(f!=-){
ans = "";
getans(f);
if(res.compare("")==) res = ans;
else if(res.length()>ans.length()||res.length()==ans.length()&&ans.compare(res)<) res = ans;
}
}
}
cout<<res<<endl;
}
}

hdu 1664(数论+同余搜索+记录路径)的更多相关文章

  1. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...

  2. HDU - 1503 最长公共子序列记录路径

    题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...

  3. HDU 1503 Advanced Fruits(LCS+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是 ...

  4. HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形

    题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...

  5. 迷宫问题 (bfs广度优先搜索记录路径)

    问题描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  6. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

  7. HDOJ-2181(深搜记录路径)

    哈密顿绕行世界问题 HDOJ-2181 1.本题是典型的搜索记录路径的问题 2.主要使用的方法是dfs深搜,在输入的时候对vector进行排序,这样才能按照字典序输出. 3.为了记录路径,我使用的是两 ...

  8. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  9. hdu 1226 BFS + bfs记录路径

    http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...

随机推荐

  1. JavaScript定义类与对象的一些方法

    最近偶然碰到有朋友问我"hoisting"的问题.即在js里所有变量的声明都是置顶的,而赋值则是在之后发生的.可以看看这个例子: 1 var a = 'global'; 2 (fu ...

  2. k好数 数位dp

    问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22 ...

  3. 如何在Linux上安装QQ

    我一直无法解决Ubuntu QQ问题,而最近我重装ubuntu之后在网络上找到与QQ相关的内容,网上有大神开发出了新版的wineQQ,解决了我们对QQ的需求.经过尝试,完成了QQ安装 如图  安装的是 ...

  4. The Shortest Path in Nya Graph HDU - 4725

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  5. git与svn与github与码云的区别

    1.git与github(https://www.oschina.net/)的区别 Git(https://git-scm.com/)是一个版本控制工具 github是一个用git做版本控制的项目托管 ...

  6. table第一行合并,其余行宽度失效问题

    <col>标签 http://www.w3school.com.cn/tags/tag_col.asp 由于表格中有一行合并过,所以其它没有合并的行的列宽就会平均化,对列宽的设置也会失效. ...

  7. 51Nod 1024 矩阵中不重复的元素 | 技巧 数学

    first try: set<LL> sset; int main() { LL m,n,a,b; while(~scanf("%lld%lld%lld%lld",&a ...

  8. 【hdu5217-括号序列】线段树

    题意:给一串括号,有2个操作,1.翻转某个括号.2.查询某段区间内化简后第k个括号是在原序列中的位置.1 ≤ N,Q ≤ 200000. 题解: 可以知道,化简后的序列一定是)))((((这种形式的. ...

  9. 【Foreign】Weed [线段树]

    Weed Time Limit: 20 Sec  Memory Limit: 512 MB Description 从前有个栈,一开始是空的. 你写下了 m 个操作,每个操作形如 k v : 若 k ...

  10. JS之document对象(找元素、操作内容、操作属性、操作样式及4道例题)

    document对象 一.找元素 1.根据id找 示例: <input id = "a" type="button" value="找元素&qu ...