代码改变世界

Book Reading

2019-09-15 15:57  木木王韦  阅读(226)  评论(0)    收藏  举报

Book Reading

Polycarp is reading a book consisting of nn

pages numbered from 11

to nn

. Every time he finishes the page with the number divisible by mm

, he writes down the last digit of this page number. For example, if n=15n=15

and m=5m=5

, pages divisible by mm

are 5,10,155,10,15

. Their last digits are 5,0,55,0,5

correspondingly, their sum is 1010

.Your task is to calculate the sum of all digits Polycarp has written down.You have to answer qq

independent queries.InputThe first line of the input contains one integer qq

(1≤q≤10001≤q≤1000

) — the number of queries.The following qq

lines contain queries, one per line. Each query is given as two integers nn

and mm

(1≤n,m≤10161≤n,m≤1016

) — the number of pages in the book and required divisor, respectively.OutputFor each query print the answer for it — the sum of digits written down by Polycarp.
Example
Input
7
1 1
10 1
100 3
1024 14
998244353 1337
123 144
1234312817382646 13
Output
1
45
153
294
3359835
0
427262129093995
这道题直接以十为单位循环计算,剩下的倍数依次计算会超时,用数组标记一下就好了。

#include<iostream>
using namespace std; 
int main(){
	long long T;
	cin>>T;	
	while(T--){		
		long long int n,m,sum[11];		
		cin>>n>>m;		
		sum[0]=0;			
			for(int i=1;i<=10;i++){			
			sum[i]=sum[i-1]+(m*i)%10;		
			}		
		long long d=n/m;		
		cout<<d/10*sum[10]+sum[d%10]<<endl;	
		}
	return 0;
}