求出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. vc 按钮自绘

    按钮自绘,将按钮区域分成三部分,左边.右边.中间都由贴图绘制,可用于手动进度条按钮,或者左右选择项按钮 cpp代码部分: // LRSkinButton.cpp : implementation fi ...

  2. vs2008中使用gdi+的设置

    vs2008中使用gdi+ 1.新建一个mfc工程 2.在stdafx.h文件中加入以下几行语句: #include <gdiplus.h>                //#pragm ...

  3. exception in thread main java.lang.NoClassDefFoundError wrong name解决方法

    当不含包层次的HelloWorld.java代码(此时程序运行正常) public class HelloWorld{ public static void main(String[] args)   ...

  4. 修改注册表添加IE信任站点及启用Activex控件

    Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/In ...

  5. C++中出现的计算机术语1

    access labels(訪问标号) 类的成员能够定义为 private,这能够防止使用该类型的代码訪问该成员. 成员还能够定义为 public,这将使该整个程序中都可訪问成员.  address( ...

  6. Delphi中复制带有String的记录结构时不能使用Move之类的内存操作函数

    请看下面的代码: program TestRecord; {$APPTYPE CONSOLE} uses  SysUtils,  Math; type  TRecordA = record    Na ...

  7. mysql 高可用方案MHA介绍

    概述 MHA是一位日本MySQL大牛用Perl写的一套MySQL故障切换方案,来保证数据库系统的高可用.在宕机的时间内(通常10—30秒内),完成故障切换,部署MHA,可避免主从一致性问题,节约购买新 ...

  8. OCaml Language Sucks

    OCaml Language Sucks OCaml Language Sucks

  9. poj 2038 Team Rankings 枚举排列

    //poj 2038 //sep9 #include <iostream> #include <algorithm> using namespace std; char s[1 ...

  10. hello MemSQL 入门安装演示样例

    一,介绍 MemSQL号称世界上最快的分布式关系型数据库,兼容mysql但快30倍,能实现每秒150万次事务.原理是仅用内存并将SQL预编译为C++. 二,部署 官网下载地址:http://www.m ...