hdu1664 Different Digits
求出n的倍数m,要求m使用的不同数字最少,且最小。
一开始不知道怎么搜,因为不知道m由多少个不同的数字组成。
然后百度了一下,看到和数论有关。
m可能使用的数字的个数可能为一个或者两个
a,aa,aaa....n+1个a, 将这些数%n,那么肯定有两个余数相等,抽屉原理。那么这两个数相减,得到的数肯定是n的倍数,且这两个数由a和0组成。
所以就知道怎么搜了,先搜m由一个数组成的情况,如果不存在,那么就搜两个数组成的情况,要注意全部搜完,因为题目要求m最小。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
struct node
{
int res;
string str;
};
bool vis[];;
int digit;
int cnt;
string ans;
bool find_; void bfs1(int n)
{
int res;
int k;
for(int i=; i<=; ++i)
{
k = ;
res = i % n;
memset(vis,,sizeof(vis));
while(!vis[res] && res!=)
{
vis[res] = true;
res = (res * + i) % n;
k++;
}
if(res==)
{
if(cnt==)
{
cnt = k;
digit = i;
}
else if(cnt>k)
{
cnt = k;
digit = i;
}
}
}
}
void bfs2(int i, int j,int n)
{
memset(vis,,sizeof(vis));
queue<node> q;
node cur,tmp;
if(i!=)
{
cur.res = i % n;
cur.str = (char)(i+'');
q.push(cur);
}
cur.res = j % n;
cur.str = (char)(j+'');
q.push(cur);
while(!q.empty())
{
cur = q.front(); q.pop();
if(cur.res ==)
{
if(!find_)
{
ans = cur.str;
find_ = true;
}
else if(cur.str.size() < ans.size())
ans = cur.str;
else if(cur.str.size()==ans.size() && cur.str < ans)
ans = cur.str;
return; }
if(find_ && cur.str.size() >= ans.size())
continue;
tmp.res = (cur.res * + i) % n;
if(!vis[tmp.res])
{
vis[tmp.res] = true;
tmp.str = cur.str + (char)(i+''); q.push(tmp);
}
tmp.res = (cur.res * + j) % n;
if(!vis[tmp.res])
{
vis[tmp.res] = true;
tmp.str = cur.str + (char)(j+'');
q.push(tmp);
}
}
} int main()
{
int n,i,j;
while(scanf("%d",&n),n)
{
find_ = false;
cnt = ;
bfs1(n);
if(cnt!=)
for(i=; i<cnt; ++i)
printf("%d",digit);
else
{
for(i=; i<=; ++i)
for(j=i+; j<=; ++j)
{
bfs2(i,j,n);
}
cout<<ans;
}
puts("");
}
}
hdu1664 Different Digits的更多相关文章
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Revolving Digits[EXKMP]
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
随机推荐
- 判断指定进程是否为x64的方法(在ntdll判断某个x64函数是否存在)
BOOL IsWow64ProcessEx(HANDLE hProcess) { // 如果系统是x86的,那么进程就不可能有x64 bool isX86 = false; #ifndef _WIN6 ...
- 使用iftop网络流量监控
iftop这是一个非常有用的工具.下面的命令监视无线网卡在我的笔记本 iftop -i wlan0 比如,我现在玩音乐视频.iftop显示的信息: 基本说明: 1. 屏幕主要部分都是表示两个机器之间的 ...
- java jquery 函数多參数传递
业务需求: 名次 伙伴 业绩 签单 面谈 每日目标 1 文彬 5100 6 10 查看目标 2 马红月 4550 4 6 查看目标 3 王刚 4100 3 9 查看目标 4 郭亚凯 3450 4 ...
- Linux入门基础 #9:管道及重定向
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- WM_SYSCOMMAND消息命令整理 good
注意:1. 使用WM_SYSCOMMAND时,鼠标的一些消息可能会受到影响,比如不能响应MouseUp事件,可以在窗口中捕获WM_SYSCOMMAND消息,并判断消息的CommandType来判断消息 ...
- 玩转Windows服务系列——命令行管理Windows服务
原文:玩转Windows服务系列——命令行管理Windows服务 说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令, ...
- java学习笔记10--枚举
java学习笔记10--枚举 在JDK1.5之前,java可以有两种方式定义新类型:类和接口.对于大部分面向对 象编程来说,这两种方法看起来似乎足够了,但是在一些特殊情况下,这些方法就不适合.例如,想 ...
- 【OpenCV新手教程第14】OpenCVHough变换:霍夫变换线,霍夫变换圆汇编
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26977557 作者:毛星云(浅墨) ...
- appium简明教程
appium简明教程 什么是appium? 下面这段介绍来自于appium的官网. Appium is an open-source tool you can use to automate mobi ...
- sdbntrjm57k
http://www.zhihu.com/collection/24337307 http://www.zhihu.com/collection/24337259 http://www.zhihu.c ...