One-two, One-two 2

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

 
Description
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 N members 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".

Sample Input

input output
5
Impossible
8
112

AC代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#define N 5000002
using namespace std;
int n;
struct Node{
int dig,mod;
int f;//模拟指针,指向数组的下一个元素
}nod[N]; int l,r;
bool vis[N];
int ret; void BFS(){
while(l<r){
int tmod1=(nod[l].mod*+)%n;
int tmod2=(nod[l].mod*+)%n;
if(!vis[tmod1]){
vis[tmod1]=;
r++;
nod[r].mod=tmod1;
nod[r].dig=;
nod[r].f=l;
if(tmod1==){ret=r;return ;}
}
if(!vis[tmod2]){
vis[tmod2]=;
r++;
nod[r].mod=tmod2;
nod[r].dig=;
nod[r].f=l;
if(tmod2==){ret=r;return ;}
} l++; }
} void out(int x)
{
if(x>)out(nod[x].f);
else if(x==)return;
cout<<nod[x].dig;
} void first()
{
nod[].dig=;
nod[].f=;
nod[].mod=;
nod[].dig=;
nod[].f=;
nod[].mod=;
} int main()
{
while(cin>>n)
{
if(n==||n==){
cout<<n<<endl;
continue;
}
first(); l=;r=;ret=;
memset(vis,,sizeof(vis));
vis[]=vis[]=; BFS();
if(ret==){
cout<<"Impossible"<<endl;
continue;
}
out(ret);
cout<<endl;
}
return ;
}

ural One-two, One-two 2的更多相关文章

  1. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  2. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  3. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  6. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  7. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  8. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  9. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

  10. ural 2064. Caterpillars

    2064. Caterpillars Time limit: 3.0 secondMemory limit: 64 MB Young gardener didn’t visit his garden ...

随机推荐

  1. 你真的知道setTimeout是如何运行的吗

    大家看下如下代码,猜猜执行结果: var start = new Date; setTimeout(function(){ console.log('时间流逝了:'+(new Date - start ...

  2. SharePoint Online 创建门户网站系列之创建栏目

    前 言 SharePoint Online的栏目,简单描述即显示在首页上的各个模块信息,这里,我们主要介绍我们首页上的栏目,包括简介类型.新闻列表类型.图片类型: 下面,让我们开始在SharePoin ...

  3. firefox中flash经常崩溃

    建议: 1.安装flashblck插件 2.添加配置文件 在C:\Windows\SysWOW64\Macromed\Flash添加mmc.cfg. mmc.cfg的内容: SlientAutoUpd ...

  4. Android 视频播放器,在线播放

    1. Bilibili https://github.com/Bilibili/ijkplayer 1.测试的时候总是崩溃,不知道是我不会用还是怎么回事. 2016-04-15 2.AndroidVi ...

  5. 监听SD卡状态

     最近在做项目时遇到需要处理SD卡拔出时的监听,在网上找了很多资料.总结了一下, 用接收广播处理最有效率     sd卡拔插时会发送广播,具体如下(摘自一位大虾的博客  来自:http://blog. ...

  6. [其他]Ubuntu安装genymotion后unable to load VirtualBox engine

    问题: Ubuntu安装genymotion后unable to load VirtualBox engine 解决办法: 如果没有安装VirtualBox,要先安装VirtualBox. 安装Vir ...

  7. Android 从Gallery获取图片

    本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  8. 【代码笔记】iOS-把<br!>换成\n

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  9. 【代码笔记】iOS-scrollerView里多个tableView加搜索框

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "customCell.h&qu ...

  10. Android 自动生成的R类

    资源文件的使用分为在代码中使用和在其他资源文件中引用该资源文件.在我们编译一个Android应用时,Android会自动生成一个R类,在该类中根据不同的资源类型又生成了相应的内部类,该类包含了系统中使 ...