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 ...
随机推荐
- VC添加背景图片 的一种方法
.如果程序是新建的对话框,要给其添加背景图片的步骤: 1)加入消息函数:afx_msg void OnPaint(); 2)BEGIN_MESSAGE_MAP(QueryDlg, CDialog) O ...
- AJAX入门---DOM操作HTML
AJAX入门---DOM操作HTML 一边学习AJAX一边坐着总结加深印象.上次写的是怎样简单的使用XMLHttpRequest对象.今天写的是DOM(文档对象模型(Document Object M ...
- 什么是VSync
VSync是垂直同期(Vertical Synchronization)的简称.主要的思路是将你的FPS和显示器的刷新率同期起来.其目的是避免一种称之为"撕裂"的现象.再以下我将具 ...
- ThinkPhp学习06
原文:ThinkPhp学习06 一.简单学习修改用户信息模块 1.编写UserAction.class.php <?php class UserAction extends Action{ pu ...
- 简单的 "双缓冲" 绘图的例子(研究一下)
所谓双缓冲就是先画到内存画布(如: TBitmap), 然后再转帖到目的地. 譬如下面小程序: procedure TForm1.FormCreate(Sender: TObject); begin ...
- 给工程师的 10 条哲理(浅薄者迷信运气,强者相信因果,软件复制成本为零,文凭不重要,AWS使得创业成本为零,每个手机都是口袋里的强大电脑)
无论是主题分布式数据库,微服务,Soylent,尤伯杯,或者矮人要塞,我们试图从物质分离出来炒作,推迟叙事的客人.与尊重有软件工程日报的社论部分客观性. 一位渠道的成员说,“当软件工程每日的意见公布, ...
- 用VLC搭建流媒体server
VLC开元项目相当强大,我们既能够将其作为播放核心用于二次开发,又能够将其作为高性能的流媒体server.今篇博客主要讲用VLC搭建流媒体server. VLC搭建流媒体server步骤非常easy: ...
- [置顶] vs2008 编译adb 支持4.2 android 系统(增加push 命令的进度)
QQ: 2506314894 本想晚些时候放出来的,但是按捺不住啊,所以修改了之后就立即放出来了.先说明一下,这次用的adb 的源码比较新的,用的vs2008 编译出来,只有一个exe 文件,直接就可 ...
- python语言学习7——数据类型和变量
整数 python可以处理任意大小的整数,包括负整数,在程序中的表示方法和数学上的写法一样 计算机由于使用二进制,有时候采用十六进制表示整数比较方便,十六进制数用0x前缀 浮点数 简单的小数就直接用小 ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...