1 ≤ N , M ≤ 10^16이므로 int → long long
M의 일의 자리로 arr 결정
M = 1, arr = 1 2 3 4 5 6 7 8 9 0
M = 2, arr = 2 4 6 8 0 2 4 6 8 0
M = 3, arr = 3 6 9 2 5 8 1 4 7 0
M = 4, arr = 4 8 2 6 0 4 8 2 6 0
M = 5, arr = 5 0 5 0 5 0 5 0 5 0
M = 6, arr = 6 2 8 4 0 6 2 8 4 0
M = 7, arr = 7 4 1 8 5 2 9 6 3 0
M = 8, arr = 8 6 4 2 0 8 6 4 2 0
M = 9, arr = 9 8 7 6 5 4 3 2 1 0
N / M → 10개
N % M → 10개 중 앞에서 부터 N % M 개
- 제목
Book Reading
- 조건
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
Polycarp is reading a book consisting of N pages numbered from 1 to N. Every time he finishes the page with the number divisible by M, he writes down the last digit of this page number. For example, if N=15 and M=5, pages divisible by m are 5,10,15. Their last digits are 5,0,5 correspondingly, their sum is 10.
Your task is to calculate the sum of all digits Polycarp has written down.
You have to answer q independent queries.
- 입력
The first line of the input contains one integer q (1≤q≤1000) — the number of queries.
The following q lines contain queries, one per line. Each query is given as two integers N and M (1≤N,M≤10^16) — the number of pages in the book and required divisor, respectively.
- 출력
For each query print the answer for it — the sum of digits written down by Polycarp.
예제 입력 1 | 7 1 1 10 1 100 3 1024 14 998244353 1337 123 144 1234312817382646 13 |
예제 출력 1 | 1 45 153 294 3359835 0 427262129093995 |
#include <iostream>
using namespace std;
int main() {
int test_case, arr[10];
long long N, M, sum, all_sum;
cin >> test_case;
for (int n = 0; n < test_case; n++) {
cin >> N >> M;
sum = all_sum = 0;
for (int m = 0; m < 10; m++) {
arr[m] = ((M % 10) * (m + 1)) % 10;
all_sum += arr[m];
}
if (N / M >= 10) {
sum += ((N / M) / 10) * all_sum;
}
for (int m = 0; m < (N / M) % 10; m++) {
sum += arr[m];
}
cout << sum << endl;
}
return 0;
}