View Code of Problem 3920

#include<iostream>
using namespace std;
//m的个位数规律 
//1 1 2 3 4 5 6 7 8 9 0 ===1 2 3 4 5 6 7 8 9 0
//2 2 4 6 8 10 12 14 16 ===2 4 6 8 0
//3 3 6 9 12 15 18 21 24 27 30 33 36 39===3 6 9 2 5 8 1 4 7 0
//4 4 8 12 16 20 24 28 ===4 8 2 6 0
//5 5 10 15 20 25 ===5 0 
//6 6 12 18 24 30 36 ===6 2 8 4 0
//7 7 14 21 28 35 42 49 56 63 70 77 ===7 4 1 8 5 2 9 6 3 0
//8 8 16 24 32 40 48 ===8 6 4 2 0
//9 9 18 27 36 45 54 63 72 81 90 99 ===9 8 7 6 5 4 3 2 1 0
int main() {
	int arr[10][10]={
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
		2, 4, 6, 8, 0, 2, 4, 6, 8, 0,
		3, 6, 9, 2, 5, 8, 1, 4, 7, 0,
		4, 8, 2, 6, 0, 4, 8, 2, 6, 0,
		5, 0, 5, 0, 5, 0, 5, 0, 5, 0,
		6, 2, 8, 4, 0, 6, 2, 8, 4, 0,
		7, 4, 1, 8, 5, 2, 9, 6, 3, 0,
		8, 6, 4, 2, 0, 8, 6, 4, 2, 0,
		9, 8, 7, 6, 5, 4, 3, 2, 1, 0
	};
	int sum[10];//m的个位数为0-9的一次规律和 
	for(int i=0; i<10; i++) {
		sum[i]=0;
		for(int j=0; j<10; j++) sum[i]+=arr[i][j]; 
	}
	int q;
	long long s,n,m;
	cin>>q;
	while(q--) {
		cin>>n>>m;
		if(m==n) cout<<m%10<<endl;
		else {
			long long t=n/(m*10);
			s=sum[m%10]*t;
			//剩下的不满一个周期 
			//n=1234312817382646
			//t=9494713979866
			//t*10*m=1234312817382580
			for(long long i=t*10*m+m; i<=n; i+=m) s+=i%10;
			cout<<s<<endl;
		}
	}
}

Double click to view unformatted code.


Back to problem 3920