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. jQuery.extend()方法和jQuery.fn.extend()方法源码分析

    这两个方法用的是相同的代码,一个用于给jQuery对象或者普通对象合并属性和方法一个是针对jQuery对象的实例,对于基本用法举几个例子: html代码如下: <!doctype html> ...

  2. 【转】Android NFC学习笔记

    一:NFC的tag分发系统 如果想让android设备感应到NFC标签,你要保证两点 1:屏幕没有锁住 2:NFC功能已经在设置中打开 当系统检测到一个NFC标签的时候,他会自动去寻找最合适的acti ...

  3. 你真的了解UIEvent、UITouch吗?

    一:首先查看一下关于UIEvent的定义 //事件类型 typedef NS_ENUM(NSInteger, UIEventType) { UIEventTypeTouches, UIEventTyp ...

  4. 自定义button

    改变button内部label和imageView的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect - (CGRect)imag ...

  5. iOS之 开发常用到的宏定义

    不久前做过一个小项目种用到了就记录下来方便自己以后使用,一个非常实用的宏定义来打印函数名称等 #ifdef DEBUG #define DebugLog(fmt, ...) NSLog((@" ...

  6. 【问题排查】StringIndexOutOfBoundsException

    工作中遇到 java.lang.StringIndexOutOfBoundsException ,查看网上资料,总结如下 1.异常定义: Java API指出StringIndexOutOfBound ...

  7. git浅谈

    我们为什么要使用git 应用场景分析 1.使用svn,已经开发完一个需求,正在开发第二个需求,但是测试需要你立刻将你完成的第一个需求提交,请问现在你该怎么做: svn的解决方法大概是这样的:打开提交视 ...

  8. 集合1--毕向东java基础教程视频学习笔记

    Day14 集合框架01 体系概述02 共性方法03 迭代器04 List集合共性方法05 ListIterator06 List集合具体对象特点07 Vector中的枚举 01 体系概述 集合类为什 ...

  9. Spring的第一个例子

    Spring 的控制翻转IoC,或者依赖注入.在测试类中没有new一个新对象,对象是从xml文件中注入的. xml文件中的<beans>是一个大容器,里面的<bean>就是容器 ...

  10. (ios) nsnotification总结

    1  文本输入,键盘显示时,view向上,键盘隐藏时,view向下 1.1 注册键盘显示,关闭通知,并实现主界面上下变动 [[NSNotificationCenter defaultCenter] a ...