Numbers and problems HRBUST-1140 Congruence Theorem

An operation is defined as: Knowing a number, repeat the sum of its digits until the remaining number is one digit and cannot be summed. For example: the number 2345, the first summation will get 2 + 3 + 4 + 5 = 14, and then the sum of the digits of 14 will get 1 + 4 = 5, and the 5 will no longer be summed. Now please find out what the final number is after performing this operation on a b .

Input

For each set of data: the
first row contains two numbers a (0 <= a <= 2000000000) and b (1 <= b <= 2000000000)

Output

For each set of data: the first line, what is the number obtained after the operation on a b is output .
Sample Input
2 3
5 4
Sample Output
8
4

abcd%9=(a+b+c+d)%9

同 余 定理
(a + b)% mod = (a% mod + b% mod)% mod
(a * b)% mod = (a% mod) (b% mod)% mod

We can split a b into (a 2 ) b/2 , and store the multiplier in k if b is odd. When b is equal to 1, the sum of all the digits is in k.
#include<iostream>
using namespace std;
int main()
{
    
    
	int a,b;
	while(cin>>a>>b)
	{
    
    
		int k=1;
		if(a==0)
		{
    
    
			cout<<0<<endl;
			continue;
		}
		while(b>=1)
		{
    
    
			if(b&1)
			k=((a%9)*(k%9))%9;
			a=((a%9)*(a%9))%9;
			b=b/2;
		}
		if(k==0)
		cout<<9<<endl;
		else
		cout<<k<<endl;
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/Huo6666/article/details/112907991