用到 1.同余定理: (a + b) % c == (a % c + b % c) % c;
2. k * 10^m % 9 == k % 9。
综上,设a == b *100 + c * 10 + d; A == b + c + d,则a % 9 == (b*100 % 9 + c*10 % 9 + d % 9) % 9 == (b + c + d) % 9 % 9 == (b + c + d) % 9 == A % 9;。
#include <cstdio>
int main(){
int t;
scanf("%d", &t);
long long a, b;
while(t-- && scanf("%lld%lld", &a, &b)){
if(!(a && b)){
printf("0\n");
continue;
}
int temp = (a % 9) * (b % 9) % 9;
printf("%d\n", temp ? temp : 9);
}
return 0;
}