Book Reading

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;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值