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. HDOJ.1075 What Are You Talking About(map)

    What Are You Talking About 点我跳转到题面 点我一起学习STL-MAP 题意分析 首先第一组START-END给出翻译的字典,第二组START-END给出一句话,查找里面出现 ...

  2. 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核

    我们在前面已经介绍了coding.net和本地git的基本用法. 但是多人协作开发时情况会复杂得多,所以我们最好有一些规范来保证项目多人开发顺利进行. 比如说 规范一 master代码分支  需要开启 ...

  3. Semphore信号量的使用

    前言:在多线程环境的同步中,我们为了让每个线程具有同步的作用,经常采用synchronize.reetrantlock等同步手段进行上锁,以便在同一时间只能有一个线程具有访问变量和读写变量的权力.然而 ...

  4. Nginx反向代理两个tomcat服务器

    第一步,在Linux上安装两个tomcat,修改好端口号后,启动起来. 第二步,配置本地的DNS解析,即修改host文件: 第三步,配置Nginx配置文件 反向代理的配置虚拟主机配置差不多也要配置虚拟 ...

  5. ACE主动对象模式(2)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/12/590153.html 我们简单的介绍了一下ACE主动对象实现方式,同时提出了两个问题: ...

  6. 如何通过反射来创建对象?getConstructor()和getDeclaredConstructor()区别?

    1. 通过类对象调用newInstance()方法,适用于无参构造方法: 例如:String.class.newInstance() public class Solution { public st ...

  7. NYOJ 747贪心+dp

    蚂蚁的难题(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:4   描述 蚂蚁终于把尽可能多的食材都搬回家了,现在开始了大厨计划. 已知一共有 n 件食材,每件食材有一个美味 ...

  8. python读书笔记-《简明python教程》上

    1月15日 <简明python教程>上 基本结构: 基础概念+控制流+函数+模块+数据结构+面向对象+I/O+异常+标准库+其他   1.概念 1-0    退出python linux: ...

  9. arm架构与体系结构

    1.cpu与soc 内核版本号与soc版本号由arm公司确定. 芯片型号由各半导体公司确定. soc包括cpu与一些基本内设.(一般提到CPU都指的是soc,实际上cpu只是soc的一部分). RIS ...

  10. JavaScript知识之判断字符串中出现最多的字符及次数

    var str = 'asdddasdfdseeeeeweeeeeeeeeeeee'; var json = {}; // 定义json一个对象 for(var i = 0; i < str.l ...