HDU 4294 Multiple(搜索+数学)

题意:

给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少。

思路:

这里用到一个结论就是任意两个数可以组成任何数的倍数。知道这个之后就可以用搜索来做了。还有一个问题就是最多找n+1个数,因为由鸽巢原理,这n+1个数当中模上n一定有一个一同的。所以他们一减就是答案。如果找到直接是它的倍数的话,就直接返回。

搜索时保存的是它的余数,如果余数为0 的时候直接返回。还有就是在搜索中并不是直接找余数相同的两个数。而是找余数为0的。当暴力不同元素个数为2的时候,这时候已经算是找余数刚开始是由两个数相减得到的了。

#include <bits/stdc++.h>

using namespace std;
const int maxn = 10005;
int n, k;
int num[2];
int fa[maxn];
char st[maxn];
bool bfs(int nums)
{
    bool vis[maxn];
    memset(vis, false, sizeof(vis));
    queue<int> Q;
    Q.push(0);
    while (!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for (int i = 0; i < nums; i++)
        {
            int v = (u * k + num[i]) % n;
            if (!vis[v] && !(u == 0 && num[i] == 0))
            {
                fa[v] = u;
                st[v] = num[i] + ‘0‘;
                vis[v] = true;
                if (v == 0) return true;
                Q.push(v);
            }
        }
    }
    return false;
}
bool cmp(string a, string b)
{
    int len1 = a.length();
    int len2 = b.length();
    if (len1 != len2) return len1 < len2;
    for (int i = 0; i < len1; i++)
        if (a[i] != b[i]) return a[i] < b[i];
    return 0;
}

bool get_str(int u, string &str)
{
    if (fa[u] != 0) get_str(fa[u], str);
    str = str + st[u];
}
void update_ans(string &ans, string &str)
{
    str = "";
    get_str(0, str);
    if (ans == "a" || cmp(str, ans))
        ans = str;
}
int main()
{
    while (~scanf("%d%d", &n, &k))
    {
        string ans = "a";
        string str = "";
        for (int i = 1; i < k; i++)
        {
            num[0] = i;
            if (bfs(1))//找右一个数字构成的数
                update_ans(ans, str);
        }
        if (ans == "a")//如果找不到一个的,就找两个的
        {
            for (int i = 0; i < k; i++)
            {
                for (int j = i + 1; j < k; j++)
                {
                    num[0] = i;
                    num[1] = j;
                    if (bfs(2))
                        update_ans(ans, str);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

  

时间: 2024-10-14 20:55:27

HDU 4294 Multiple(搜索+数学)的相关文章

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 positiv

HDU 1005 Number Sequence (数学规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104190    Accepted Submission(s): 25232 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathys

HDU 4937 Lucky Number (数学,进制转换)

题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); //把[first0,last0)之间的部分替换成[first,last)之间的字符串 /* 题意: 我们将3,4,5,6认为是幸运数字.给定一个十进制数n. 现在可以讲起任意转

HDU 1018 Big Number 数学题解

Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of

hdu 4823 Energy Conversion(数学)

题目链接:hdu 4823 Energy Conversion 题目大意:中文题,不解释. 解题思路:首先判断一下m是否已经大于n了,如果大于那么就是0,假设中间变换的各个值为ai,那么bi=ai+c,bi数组为等比数组(可推),所以就有了cnt=log((n+c)a)log(double(k)),结果为浮点数,需要向上取整. #include <cstdio> #include <cstring> #include <cmath> int main () { int

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

HDU 2828 DLX搜索

Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 771    Accepted Submission(s): 230 Special Judge Problem Description There are several switches and lamps in the room, however, the connecti

HDU 1518 Square 搜索

Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 11   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a set of sticks of vario