【u236】火炬

Time Limit: 1 second
Memory Limit: 128 MB

2008北京奥运会,你想成为四川汶川的一名火炬手,结果层层选拔,终于到了最后一关,这一关是一道很难的题:任意给定一个正整数N(N<=100000),求一个最小的正整数M,使得N*M的十进制表示形式里只含有1和0.

【输入格式】

一行,输入一个整数N。

【输出格式】

输出一行,如果有解,输出最小的M,否则输出“No Solution”

【数据规模】

100%的数据保证答案不超过1000000。

Sample Input1

12
Sample Output1

925

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u236

【题解】

题目给的最后的答案范围不对;
最后的答案最大可能为14708277972909
(并不是题目描述的100W);
所以两个数的乘积会非常大;
但数据会保证两个数的乘积用long long能存的下(最后答案很大的话它相应的输入的N减小了一点);
做法:
首先找到第一个大于n的数字x(x满足所有的数字仅有0和1构成);
之后再用二进制进位的规则不断地给这个数字加1就好;
比如
101001
+1
101010
只不过不是真的加1;比如上面实际上就是加10了;
且这样总能满足数字全由0和1组成;
且为最小;(这样加最多只要加20W次,妥妥地不超);
然后看看能不能被n整除
如果能
就直接输出这个数字除m的结果;就是答案了;
然后结束程序;
//如果直接按照十进制加1枚举,每次还要判断这个数字是不是全由01组成;而且枚举量是巨大的;实际上最多要枚举1e18次。。呵呵

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 10+20;
const LL INF = 8e18;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

string s;
int a[MAXN];
int len;

LL fi(int b[MAXN])
{
    LL t = 0;
    rep2(j,len,1)
        t=t*10+b[j];
    return t;
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    cin >>s;
    reverse(s.begin(),s.end());
    len = s.size();
    rep1(i,1,len)
        a[i] = s[i-1]-'0';
    LL n = fi(a);
    rep1(i,1,len)
    if (a[i]>1)
    {
        int j = i+1;
        while (j<=len && a[j]>1) j++;
        j--;
        a[j]=0;a[j+1]++;
        rep1(k,1,j-1)
            a[k] = 0;
    }
    if (a[len+1]>0)len++;
    LL t=fi(a);
    while (t<INF)
    {
        if (t%n==0)
        {
            LL ans = t/n;
            cout << ans << endl;
            return 0;
        }
        a[1]++;
        rep1(i,1,len)
            if (a[i]>1)
                a[i+1]++,a[i] = 0;
        if (a[len+1]>0) len++;
        t = fi(a);
    }
    puts("No Solution");
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(223)  评论(0编辑  收藏  举报