首页 > 其他分享 >kuangbin专题一 简单搜索 找倍数(POJ-1426)

kuangbin专题一 简单搜索 找倍数(POJ-1426)

时间:2023-04-14 23:34:43浏览次数:38  
标签:typedef string 1426 ll long POJ kuangbin pair include

Find The Multiple

Time Limit: 1000MS Memory Limit: 10000K

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111


题目大意

多组数据,每组数据给一个整数 n ,求出一个只包含0和1的且是 n 的倍数的数字。



解题思路

思路一

很明显的搜索题,对于这个问题,暂时不知道是否会有爆 long long 的风险。所以一开始我选择了用string来存这个倍数的最终结果,每次只需要判断当前的数字 mod n是否为0即可,如果余数不为0,则继续对这个余数进行加一位0或1进行搜索;如果余数为0,即找到了这个倍数。由于这个倍数的长度是未知的,所以应当采用迭代加深,即IDDFS或者BFS来解决这个问题。BFS较为熟悉且也很容易写,故我一开始写下了如下代码:

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;


ll n;

string bfs(){
    queue<psi> q;
    q.push(make_pair("1", 1));

    while(!q.empty()){
        string cur = q.front().first;
        ll r = q.front().second;
        q.pop();

        if(r == 0) return cur;    //当余数为0 找到倍数
        //两种状态入队
        q.push(make_pair(cur + "0", (r * 10) % n));  
        q.push(make_pair(cur + "1", (r * 10 + 1) % n));
    }

    return "";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    while(cin >> n && n){
        string res = bfs();
        cout << res << endl;
    }

    return 0;
}

思路二

但是事实上,思路一的代码在POJ上,我记得是过不了的,会MLE,不过在其他平台上也许可以AC。

所以需要找到一种方式来优化存储。实际上,n的范围还是比较小的,经过打表实验,结果发现其并不会爆 long long。

随便写了个打表的程序,大概都不会爆 long long 的(写得有点烂):

bool has_other(ll x){
    string s = to_string(x);
    for(auto &ch : s)
        if(ch != '0' && ch != '1')
            return true;
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    for(int i = 1; i <= 200; i ++ ){
        for(ll j = 1; j <= 100000000000; j ++ ){
            ll res = i * j;
            if(!has_other(res)){
                cout << i << ' ' << res << endl;
                break;
            }
        }
    }
    
    return 0;
}

由此可知,我们可以不用string来存储最终结果,直接开long long放入队列,找到最小倍数即可。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;


ll n;

ll bfs(){
    queue<ll> q;
    q.push(1);

    while(!q.empty()){
        ll r = q.front();
        q.pop();

        if(r % n == 0) return r;

        q.push(r * 10);
        q.push(r * 10 + 1);
    }

    return -1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    while(cin >> n && n){
        ll res = bfs();
        cout << res << endl;
    }

    return 0;
}

标签:typedef,string,1426,ll,long,POJ,kuangbin,pair,include
From: https://www.cnblogs.com/MAKISE004/p/17320241.html

相关文章

  • poj2777(线段树)
    CountColorPOJ-2777思路:暴力能过,线段树维护这个区间的颜色,如果是混色则置为1,如果是单一颜色则设为这个颜色,修改就是正常的区间修改,区间查询就要变一下。还有题解是用二进制做得,可以学一下。#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<fstream>#inc......
  • kuangbin专题一 简单搜索 翻转(POJ-3279)
    FliptileTimeLimit:2000MS MemoryLimit:65536KDescriptionFarmerJohnknowsthatanintellectuallysatisfiedcowisahappycowwhowillgivemoremilk.HehasarrangedabrainyactivityforcowsinwhichtheymanipulateanM×Ngrid(1≤M≤15;1≤......
  • poj 2182
    LostCowsPOJ-2182与这题一样BuyTickets-POJ2828-VirtualJudge(csgrandeur.cn)题意:有1~NN个数字,这N个数字的顺序是打乱的,从第二个数字开始给你它的前面有多少个数字比他小思路:输入的数字都要加一,然后我们从后往前遍历,在线段树中如果左子树的sum‘>sum,则进入左子......
  • kuangbin专题一 简单搜索 抓住那头牛(POJ-3278)
    CatchThatCowTimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:210291 Accepted:63838DescriptionFarmerJohnhasbeeninformedofthelocationofafugitivecowandwantstocatchherimmediately.HestartsatapointN(0≤N≤100,000)on......
  • kuangbin专题一 简单搜索 棋盘问题(POJ-1321)
    棋盘问题TimeLimit:1000MS MemoryLimit:10000KTotalSubmissions:125427 Accepted:56304Description在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘......
  • kuangbin专题一 简单搜索 地牢大师(POJ-2251)
    DungeonMasterTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:92499 Accepted:31990DescriptionYouaretrappedina3Ddungeonandneedtofindthequickestwayout!Thedungeoniscomposedofunitcubeswhichmayormaynotbefilledwith......
  • POJ 1780 Code (欧拉回路+非递归版dfs)
    题目地址:POJ1780还是求序列的欧拉回路。只不过这题有两坑。第一坑是用数字来当点的话,会MLE,因为每个数字可以连10条边,100w条边会MLE,即使用vector也会TLE。这题可以用边来记录,对于n为1时直接输出,然后后面的,比如12,23这两个点就用边权值为123来表示这两个点,这样就把点和边的范围......
  • HDU 1116 && POJ 1386 Play on Words(欧拉路径)
    按字母来建边,每个单词的首字母和尾字母加边。先判断是否连通,然后判断每个字母的入度和出度不能出现差的绝对值大于2,然后入度和出度差的绝对值为1的不能超过两个。就可以形成欧拉路径代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include......
  • POJ 1753 Flip Game (高斯消元)
    题目地址:POJ1753第三次做这道题了。第一次是刚学搜索的时候做的,第二次是刚学状态压缩枚举的时候做的,这次是刚学高斯消元、、每次都做得很艰辛。。目测这题应该没了别的方法了吧。。。。。。这题除了高斯消元外还需要枚举变元,方法是状态压缩枚举,然后返回去迭代解方程。代码如下:#inc......
  • POJ 1830 开关问题 (高斯消元)
    题目地址:POJ1830高斯消元第一发。一个地方逻辑判断出现了失误,调了一下午啊。。。通过高斯消元来找矩阵的秩,然后2^(自由元的数量)就是答案。因为对于每个自由元,都有0和1两种状态可选。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#includ......