URAL 1495. One-two, One-two 2(暴力枚举)

18 篇文章 0 订阅

题目大意:给你一个n,让你找一个最小的十进制的数字,数字的位数在30位以内,这个数字必须是是n的整数倍。

一开始想纯暴力:2^30TLE,又想开一个30*10^6的数组dp一下,结果MLE。

鹏哥提供了一个好的思路,把30拆开暴力,先暴力后15个然后再暴力枚举前15个,这样的话就保证既不超市也不MLE了啊,注意超过15为的数字的余数只能从后面的十五位里面选。

PS:各种手残。。。

1495. One-two, One-two 2

Time limit: 2.0 second
Memory limit: 64 MB
A year ago the famous gangster Vito Maretti woke up in the morning and realized that he was bored of robbing banks of round sums. And for the last year he has been taking from banks sums that have only digits 1 and 2 in their decimal notation. After each robbery, Vito divides the money between  Nmembers of his gang. Your task is to determine the minimal stolen sum which is a multiple of  N.

Input

The input contains the number  N (1 ≤  N ≤ 10 6).

Output

Output the minimal number which is a multiple of  N and whose decimal notation contains only digits 1 and 2. If it contains more than 30 digits or if there are no such numbers, then output "Impossible".

Samples

input output
5
Impossible
8
112

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 1000010;

struct node
{
    LL num;
    int flag;
} ;

node f[maxn], pp[maxn];

bool vis[maxn];
queue<LL>fp;
int main()
{
    LL n;
    while(cin >>n)
    {
        for(int i = 0; i <= maxn-5; i++)
        {
            f[i].flag = 0;
            pp[i].flag = 0;
        }
        memset(vis, false, sizeof(vis));
        while(!fp.empty()) fp.pop();
        fp.push(1LL);
        fp.push(2LL);
        LL x;
        LL tmp = 222222222222222LL;
        LL y = 1000000000000000LL;
        int flag = 0;
        while(!fp.empty())
        {
            x = fp.front();
            fp.pop();
            if(x > tmp) continue;
            int ans = x%n;
            if( x < y && x > y/10LL)
            {
                if(!pp[ans].flag)
                {
                    pp[ans].num = x;
                    pp[ans].flag = 1;
                }
                else pp[ans].num = min(pp[ans].num, x);
                vis[ans] = true;
            }
            if(f[ans].flag) f[ans].num = min(f[ans].num, x);
            else
            {
                f[ans].num = x;
                f[ans].flag = 1;
            }
            if(ans == 0) break;
            fp.push(x*10LL+1LL);
            fp.push(x*10LL+2LL);
        }
        if(f[0].flag)
        {
            cout<<f[0].num<<endl;
            continue;
        }
        while(!fp.empty()) fp.pop();
        fp.push(1LL);
        fp.push(2LL);
        LL Min = tmp;
        int xt;
        while(!fp.empty())
        {
            x = fp.front();
            fp.pop();
            if(x > tmp) continue;
            int ans = ((x%n)*(y%n))%n;
            int p = n-ans;
            if(vis[p])
            {
                flag = 1;
                Min = min(Min, x);
                xt = p;
                break;
            }
            fp.push(x*10LL+1LL);
            fp.push(x*10LL+2LL);
        }
        if(flag) cout<<Min<<pp[xt].num<<endl;
        if(!flag) cout<<"Impossible"<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值