现在的位置: 首页 > 综合 > 正文

nyoj 485

2018年04月26日 ⁄ 综合 ⁄ 共 957字 ⁄ 字号 评论关闭
 
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 *            起首要知道:k*10^m %9=k (¥) (这个斗劲好证实,10……0%9 = 1) 设n的各位数相加得f(n),有n%9
	 *            = f(n)%9。证实如下: 中国残剩定理(a+b)%c = (a%c+b%c)%c,将式子(¥)代入,有(a+b)%9 =
	 *            (a*10%9 + b*1%9)%9 ,等价于f(ab) %9 = ab %9,即f(n) %9=n%9。
	 *            还有(a*b)%c = ((a%c)*(b%c))%c。证实如下:
	 *            令a = x*c + ayu,b = y*c + byu,所以(a*b)%c =
	 *            ((x*c+ayu)*(y*c+byu))%c = (ayu*byu)%c = ((a%c)*(b%c))%c。
	 *            
	 *            好,可以开端评论辩论题目了。令a*b = n,则n到f(n) ,f(n)到f( f(n) )
	 *            ……知道成果为个位数停止,我们假设f( f(n) )是个位数了(为了评论辩论便利),则ans =f( f(n) )%9 =
	 *            f(n) %9,f(n)%9 = n %9,n%9 = (a*b)%9 = ((a%9)*(b%9))%9。所以成果ans
	 *            = ((a%9)*(b%9))%9
	 * 
	 *            细节,若是%9得0那么成果是9,除非a,b中至少有一个为0.
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scanner = new Scanner(System.in);
		int time = scanner.nextInt();
		while (time-- != 0) {
			String str1 = scanner.next();
			String str2 = scanner.next();
			int sum = 0, sum1 = 0, i;
			for (i = 0; i < str1.length(); i++)
				sum += str1.charAt(i) - '0';
			for (i = 0; i < str2.length(); i++)
				sum1 += str2.charAt(i) - '0';
			if (sum == 0 || sum1 == 0)
				System.out.println(0);
			else {

				int x = (sum % 9 * sum1 % 9) % 9;
				System.out.println(x == 0 ? 9 : x);
			}

		}

	}
}        

抱歉!评论已关闭.