FOJ 1675 The Seventy-seven Problem

The Seventy-seven Problem
Time Limit:2s Memory limit:32M
Accepted Submit:20 Total Submit:36

One day, daxia writes a big number N that can be divided by seventy-seven on the paper.
But oaiei is careless; he spilt some ink on the number, so that the four continuous digits can not be read clearly.
Now daxia asks you to calculate how large the number N could be.

Input

There are multiple test cases. For each test case, the first line contains a big integer N (10^5<=N<=10^1000000) which had lost four continuous digits, the lost four continuous digits are represented as "xxxx".

Output

For each test case, you should output the number N, so that N is the maximum number that can be divided by seventy-seven.

Sample Input

1xxxx
1234567xxxx7654321

Sample output

19943
123456799337654321

Original: FOJ月赛-2008年12月








题目意思很简单就是叫你把xxxx替换成0000~9999中的一个数,使得数串%77=0,并且替换成的数是最大的~


一开始可能会想到找规律,确实这题规律比较明显,可以用找规律的方法做出来,这里不累述了



下面要介绍的是通常的解法

首先必须知道一点数论的基本公式  (a-b) %c =0 -----> a%c=b%c


于是就可以先把xxxx变成9999。


1234567xxxx7654321--->123456799997654321   
123456799997654321 % 77 =44
- ????0000000
如果能找到这样的数,该数%77= 123456799997654321 % 77 =44,那么只要把9999-????就可以了

很容易知道00660000000 % 77=44,并且????0000000在这种数中是最小的,所以呢就把9999 - 66 = 9933
so 答案出来了
于是第一步是先把xxxx->9999,求出这时候的结果mod
????后面的0的个数可以很容易的通过o(n)扫描得到,如果后面有K个0,那么枚举数i从0000~9999,计算 (i * 10^K) mod 77

一直枚举直到
(i * 10^K) mod 77 = mod,退出,改变9999->9999-i
于是就AC了

注意必须使用比较快速的算法计算a^b mod c

你可能感兴趣的:(c,算法,Integer,input,each,output)