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. 安装 Ubuntu 后的个人常用配置

    在 ASA 猪队友的带领下,拥抱开源世界,用上了Ubuntu.资深强迫症现身说法,配置符合自己使用习惯的Ubuntu. 1. 窗口标题栏显示菜单项 打开系统设置->外观->行为,在[显示窗 ...

  2. FM四舍五入_从小数点最后一位进位

    原贴地址:http://jiahongguang12.blog.163.com/blog/static/334665720071060551591/ 输入参数12.5445,因此FM从小数点最后一位进 ...

  3. SharePoint 2013 搜索报错"Unable to retrieve topology component health. This may be because the admin component is not up and running"

    环境描述 Windows 2012 R2,SharePoint 2013(没有sp1补丁),sql server 2012 错误描述 搜索服务正常,但是爬网一直在Crawling Full,但是爬不到 ...

  4. 动态改变actionbar上menu的图标

    工作中遇到了一个需求是,在滚动的时候让actionbar上的图标进行变色.实现后在这里总结下思路. 一.先在主题中定义好Actionbar的style <style name="App ...

  5. [Android]AndroidInject增加sqlite3数据库映射注解(ORM)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3623050.html AndroidInject项目是我写的一 ...

  6. JSON 转javabean 利器

    别再对着json来手写javabean啦.这个工作完全不要脑子,而且耗时. 这里给大家提供三种方式: android studio版: 万能的插件:GsonFormat 如何安装? Preferenc ...

  7. 我的BluetoothChat示例源码阅读笔记

    BluetoothChat核心类BluetoothChatService,该类用于管理与其他设备的蓝牙连接和设置.该类包含AcceptThread.ConnectedThread.ConnectThr ...

  8. Android java传递int类型数组给C

    接着前面的文章<Android java传递int类型数据给C><Android java传递string类型数据给C>,继续实践 实现public native int[] ...

  9. Android中的XML解析

    在安卓中主要有三种XML文档解析方式:DOM(Document Object Model), SAX(Simple API for XML), PULL 他们的主要特点如下表:   特点 主要类 DO ...

  10. 深入.net(集合)

    集合技术: 用于“批量数据”管理的重要技术,是数组技术的替代技术! 与数组技术的对比: 数组:只提供“存储的空间”,但缺乏各种数据管理措施! 集合:在数组的基础上,提供丰富的“属性”和“方法”,来方便 ...