zoukankan      html  css  js  c++  java
  • HDU 4294 Multiple

    Multiple

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1071    Accepted Submission(s): 273


    Problem Description
      Given a positive integer N, you’re to solve the following problem:
      Find a positive multiple of N, says M, that contains minimal number of different digits in base-K notation. If there’re several solutions, you should output the numerical smallest one. By saying numerical smallest one, we compar their numerical value, so 0xAhex < 11dec.
      You may assume that 1 <= N <= 104 and 2 <= K <= 10.
     
    Input
    There’re several (less than 50) test cases, one case per line.
    For each test case, there is a line with two integers separated by a single space, N and K.
    Please process until EOF (End Of File).
     
    Output
    For each test case, you should print a single integer one line, representing M in base-K notation,the answer.
     
    Sample Input
    10 8
    2 3
    7 5
     
    Sample Output
    2222
    2
    111111
     
    Source
     
    解题:据说:

    最多用两个数就可以表示任何数的倍数。

    证明 :对于一个数字a,可以构造出的数字有

    a,aa,aaa,aaaa,aaaaa,……

    每一个数对于n都有一个余数,余数最多有n个,根据鸽巢原理,前n+1个数中,必然有两个余数相等

    那么二者之差,必定为n的倍数,形式为a……a0……0。

    然后暴力搜索即可

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 20010;
     4 string ans,str;
     5 int n,k,m,num[2],pre[maxn],b[maxn];
     6 bool bfs() {
     7     queue<int>q;
     8     memset(b,-1,sizeof b);
     9     for(int i = 0; i < m; ++i) 
    10         if(num[i]) {
    11             q.push(num[i]);
    12             b[num[i]] = num[i];
    13         }
    14     memset(pre,-1,sizeof pre);
    15     while(!q.empty()) {
    16         int u = q.front();
    17         q.pop();
    18         if(!u) return true;
    19         for(int i = 0; i < m; ++i) {
    20             int t = (u*k + num[i])%n;
    21             if(b[t] == -1) {
    22                 b[t] = num[i];
    23                 pre[t] = u;
    24                 q.push(t);
    25             }
    26             if(!t) return true;
    27         }
    28     }
    29     return false;
    30 }
    31 void Path(int u) {
    32     if(pre[u] != -1) Path(pre[u]);
    33     str += (char)(b[u] + '0');
    34 }
    35 bool cmp(string &a,string &b) {
    36     if(b.size() == 0) return true;
    37     if(a.size() > b.size()) return false;
    38     if(a.size() < b.size()) return true;
    39     return a < b;
    40 }
    41 int main() {
    42     while(~scanf("%d%d",&n,&k)) {
    43         if(n < k) {
    44             printf("%d
    ",n);
    45             continue;
    46         }
    47         bool  flag = false;
    48         ans = "";
    49         for(int i = m = 1; i < k; ++i) {
    50             num[0] = i;
    51             if(bfs()) {
    52                 str = "";
    53                 Path(0);
    54                 flag = true;
    55                 if(cmp(str,ans)) ans = str;
    56             }
    57         }
    58         if(!flag) {
    59             m = 2;
    60             for(int i = 1; i < k; ++i) {
    61                 num[1] = i;
    62                 for(int j = 0; j < i; ++j) {
    63                     num[0] = j;
    64                     if(bfs()) {
    65                         str = "";
    66                         Path(0);
    67                         if(cmp(str,ans)) ans = str;
    68                     }
    69                 }
    70             }
    71         }
    72         cout<<ans<<endl;
    73     }
    74     return 0;
    75 }
    View Code
  • 相关阅读:
    程序员版孔乙己
    痛!痛!痛!我们的好兄弟Git,一路走好!
    谈谈中台架构之交易中台
    一个单例还能写出花来吗?
    为什么数据库字段要使用NOT NULL?
    阿里二面:什么是mmap?
    退税不完全操作指南,我这样操作省了2000块!
    开源组件编排引擎LiteFlow发布里程碑版本2.5.0
    开源框架TLog核心原理架构解析
    对乱糟糟的日志说再见
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4777247.html
Copyright © 2011-2022 走看看