求出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的更多相关文章

  1. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  2. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  4. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. Revolving Digits[EXKMP]

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  9. 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 ...

随机推荐

  1. jar包生制作几种方法,jar包导出三种方法:eclipse导出、jar命令、FatJar插件

    Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法 方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三 ...

  2. jqueryUI中datepicker的使用,解决与asp.net中的UpdatePanel联合使用时的失效问题

    1.jqueryUI的datepicker的使用 -->首先在jqueryUI官网上根据你的需要下载适合你系统主题的样式,jqueryUI主题下载地址: -->下载后的文件 jquery- ...

  3. mysql update改动多条数据

    通常情况下,我们会使用下面SQL语句来更新字段值: 复制代码代码例如以下: UPDATE mytable SET myfield='value' WHERE other_field='other_va ...

  4. C#控件系列--文本类控件

    C#控件系列--文本类控件         文本类控件主要包含Label.LinkLabel.Button.TextBox以及RichTextBox. Label 功能         Label用来 ...

  5. uva Matrix Decompressing (行列模型)

    Matrix Decompressing 题目:    给出一个矩阵的前i行,前j列的和.要求你求出满足的矩阵. 矩阵的数范围在[1,20].   一開始就坑在了这里.没读细致题目. 囧...   事 ...

  6. java OOP及相关基础知识汇总(转)

    OOP 对象有三个要素 behavior 接口是怎样的,有什么方法/field可以用? state 调用方法的时候,对象会有什么反应? 只有通过调用方法才能改变一个对象的state identity ...

  7. linux kernel的函数与抽象层

    在数学领域,函数是一种关系,这种关系使一个集合里的每一个元素对应到另一个(可能相同的)集合里的唯一元素. 在C语言中函数也有这种联系.自变量影响着因变量. 在linux内核驱动编程经常会有抽象层的概念 ...

  8. 传智播客成都java培训中心秀就业

    传智播客成都java培训中心秀就业 2013年被称为"史上最难就业季",成都传智播客学员如何应对的呢? 成都传智播客的学员在工作经验上颇占优势,我们采用项目驱动式教学模式,具有多年开发实战经验及教学经 ...

  9. android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)

    先上效果图: Layout为: <? xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  10. hdu 1086(判断线段相交)

    传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...