Problem address: http://acm.zjut.edu.cn/ShowProblem.aspx? Showid = 1010
A large number is used to calculate the remainder of a single digit.
It mainly finds out the rule.
M = 1, and the remainder is 0.
When m = 2, the remainder of the single digit pair 2 is the remainder.
When M = 3, the sum of all digits and the sum of three is the remainder.
When M is 4, the remainder of the first two digits is the remainder of 4.
When M is 5, the remainder of a single digit is 5.
When M = 6, the remainder of 2 and 3 is calculated as R2 and R3 respectively.
R2/R3 0 1 2
0 0 4 2
1 3 1 5
M = 7, see the following rules
10% 7 = 3, 10 ^ 2% 7 = 2, 10 ^ 3% 7 = 6, 10 ^ 4% 7 = 4, 10 ^ 5% 7 = 5, 10 ^ 6% 7 = 1;
10 ^ 7% 7 = 3, 10 ^ 8% 7 = 2 ,........
Therefore, as long as the number starts from the single digit, the number corresponding to the above is multiplied by the number of the given number, and all the results add up and then perform the remainder on 7, that is, the remainder.
When M is 8, the remainder of the first three digits is the remainder of 8.
M = 9, the sum of all digits. If the result is greater than one digit, the sum of all digits is repeated until the remaining one digit is the remainder.
Paste the following code:
// The remainder functions are described above.
# Include <iostream>
# Include <cstring>
Using namespace STD;
Int mod1 (char N [1, 105])
{
Return 0;
}
Int mod2 (char N [1, 105])
{
If (N [strlen (N)-1] % 2 = 0) return 0;
Else return 1;
}
Int mod3 (char N [0, 105])
{
Int I, Len = strlen (N), sum;
For (sum = 0, I = 0; I <Len; I ++)
{
Sum + = N [I]-'0 ';
}
Return sum % 3;
}
Int mod4 (char N [1, 105])
{
Int sum, Len = strlen (N );
If (LEN = 1)
{
Sum = N [0]-'0 ';
}
Else
{
Sum = N [len-1]-'0' + 10 * (N [len-2]-'0 ');
}
Return sum % 4;
}
Int mod5 (char N [1, 105])
{
Int Len = strlen (N );
Return (N [len-1]-'0') % 5;
}
Int mod6 (char N [1, 105])
{
Int A = mod2 (N), B = mod3 (N );
If (A = 0)
{
If (B = 0) return 0;
Else if (B = 1) return 4;
Else return 2;
}
Else
{
If (B = 0) return 3;
Else if (B = 1) return 1;
Else return 5;
}
}
Int mod7 (char N [1, 105])
{
Int I, j, Len = strlen (N), sum = 0;
Int A [6] = {1, 3, 2, 6, 4, 5 };
For (I = len-1, j = 0; I> = 0; I --, J ++)
{
Sum + = A [J % 6] * (N [I]-'0 ');
}
Return sum % 7;
}
Int mod8 (char N [1, 105])
{
Int sum, Len = strlen (N );
If (LEN = 1)
{
Sum = N [0]-'0 ';
}
Else if (LEN = 2)
{
Sum = N [len-1]-'0' + 10 * (N [len-2]-'0 ');
}
Else
{
Sum = N [len-1]-'0' + 10 * (N [len-2]-'0') + 100 * (N [len-3]-'0 ');
}
Return sum % 8;
}
Int mod9 (char N [1, 105])
{
Int I, Len = strlen (N), sum, temp;
For (sum = 0, I = 0; I <Len; I ++)
{
Sum + = N [I]-'0 ';
}
While (sum> 9)
{
Temp = sum/10;
Sum = temp + sum-Temp * 10;
}
Return sum;
}
Int main ()
{
Char N [2, 105];
Int m;
While (scanf ("% S % d", N, & M )! = EOF)
{
If (M = 1) M = mod1 (N );
Else if (M = 2) M = mod2 (N );
Else if (M = 3) M = mod3 (N );
Else if (M = 4) M = mod4 (N );
Else if (M = 5) M = mod5 (N );
Else if (M = 6) M = mod6 (N );
Else if (M = 7) M = mod7 (N );
Else if (M = 8) M = mod8 (N );
Else if (M = 9) M = mod9 (N );
Printf ("% d/N", M );
}
Return 0;
}