UVa 550 - Multiplying by Rotation
1题目
===================
Multiplying by Rotation |
Warning: Not all numbers in this problem are decimal numbers!
Multiplication of natural numbers in general is a cumbersome operation. In some cases however the product can be obtained by moving the last digit to the front.
Example: 179487 * 4 = 717948
Of course this property depends on the numbersystem you use, in the above example we used the decimal representation. In base 9 we have a shorter example:
17 * 4 = 71 (base 9)
as (9 * 1 + 7) * 4 = 7 * 9 + 1
Input
The input for your program is a textfile. Each line consists of three numbers separated by a space: the base of the number system, the least significant digit of the first factor, and the second factor. This second factor is one digit only hence less than the base. The input file ends with the standard end-of-file marker.Output
Your program determines for each input line the number of digits of the smallest first factor with the rotamultproperty. The output-file is also a textfile. Each line contains the answer for the corresponding input line.Sample Input
10 7 4 9 7 4 17 14 12
Sample Output
6 2 4
Miguel A. Revilla
1998-03-10
===================
2思路
题目大意是给定数制base,因子x的最后一位f1,因子f2,求满足x * f2 = f3的最小的f1 的位数,其中f3是将f1末位移动到f1首位得到的数。这其实是个计算过程的模拟题, 设a1为f1的当前位数字,通过(f2*a1+c)%base不断得到f1中新的位的值a2。a2是f3倒数 第k位,也是x倒数第k+1位。当此值等于f1且没有进位时,运算终止。
3代码
/*
* Problem: 550 - Multiplying by Rotation
* Lang: ANSI C
* Time: 0.026
* Author: minix
*/
#include <stdio.h>
int main() {
long long base, f1, f2;
long long a1, a2, c, n;
while (scanf ("%lld%lld%lld", &base, &f1, &f2) != EOF) {
a1 = f1; c = 0; n = 1;
while (1) {
a2 = (f2 * a1 + c) % base;
c = (f2 * a1 + c ) / base;
if (a2 == f1 && c ==0) break;
a1 = a2;
n ++;
}
printf ("%lld\n", n);
}
return 0;
}