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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值