算法练习:URAL 1495 One-two, One-two 2
来源:互联网 发布:mt4交易软件 编辑:程序博客网 时间:2024/06/05 12:39
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 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 output5 Impossible
8 112
解析:令y = x * 10 + i,其中i等于1或2,这样可以逐一求出余数,若余数为零,则返回由1和2组成的字符串。不过因为数据会很大,为避免超时,要利用取模公式做一些化简。
y%n = (10x + i)%n
= ((10x)%n + i%n)%n
= ((10%n * x%n)%n + i%n)%n
= ((10%n * x.mod)%n + i%n)%n // ((a*b) mod p*c) mod p = (a*(b*c) mod p) mod p
= (((1 * 10)%n * x.mod)%n + i%n)%n
= ((1 * (10 * x.mod)%n)%n + i%n)%n //(a mod p) mod p = a mod p
= ((10 * x.mod)%n + i%n)%n
= (10 * x.mod + i)%n
则y.mod = (10 * x.mod + i)%n
另外,当遇到余数一样时,则不用考虑后出现的数,因为相同的余数中,先出现的倍数比较小。
代码参考:http://blog.csdn.net/kewowlo/article/details/43346497
- 算法练习:URAL 1495 One-two, One-two 2
- URAL 1495. One-two, One-two 2
- URAL 1495. One-two, One-two 2
- 【构造】 URAL 1495 One-two, One-two 2
- URAL 1495 One-two, One-two 2 (BFS)
- 算法练习2:Two fighters, one winner.
- URAL 1495. One-two, One-two 2 DP or BFS?
- URAL 1495. One-two, One-two 2(暴力枚举)
- Timus 1495. One-two, One-two 2
- URAL 1216 Two Pawns and One King
- One or Two
- test one two three
- Two minus one 游戏规则
- Two minus one Rules
- 12289 - One-Two-Three
- one+two问题
- one,two,three
- 4-2-Two Stacks In One Array
- 文件夹添加右键DOS快捷入口
- CSU1005 Binary Search Tree analog
- android多线程详解之Handler
- Mac下使用mysql-server
- android环境大全
- 算法练习:URAL 1495 One-two, One-two 2
- Opencv Mat的数据读取
- 【bzoj3224】Tyvj 1728 普通平衡树
- CodeForces 377A
- Appium——iOS自动化测试
- SQL Server 2012基本语句(带实例)
- systemctl开机启动wpa_supplicant
- iOS经典讲解之Objective-C属性的内存管理原理
- android Gallery模拟左右滑动的listView