【Codeforces】1213C – Book Reading

正文索引 [隐藏]

Problem Description:

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.

Input Specification:

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.

Output Specification:

For each query print the answer for it — the sum of digits written down by Polycarp.

Sample Input:

7
1 1
10 1
100 3
1024 14
998244353 1337
123 144
1234312817382646 13

Sample Output:

1
45
153
294
3359835
0
427262129093995

解题思路:

这道题目的大意是在[1, n]这个区间内,若存在某个数 i 是m的倍数,则把 i 的个位数加起来的和sum进行输出。我一开始的想法就是暴力破解就完事啦,毫无疑问地直接TLE了。没点办法 只能开动我的小脑筋啦。先把所有能整除m的数的个位数列出来,然后计算一共有多少个数会出现以这个尾数结尾的情况,用sum += cnt*d(表示有cnt个以d为个位数且能整除m的数),最后将sum进行输出即可。

AC代码:TLE代码:

#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;
 
int main()
{
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    while(q--)
    {
        ll n, m, sum = 0;
        cin >> n >> m;
        Up(i,1,n)
        {
            if(i%m == 0)
            {
                sum += i%10;
            }
        }        
        cout << sum << endl;
    }
    return 0;
}

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;

int main()
{
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    while(q--)
    {
        ll n, m, sum = 0;
        cin >> n >> m;
        ll num = n/m;
        Up(i,1,9)
        {
            ll d = (m*i)%10;  //d表示m可能的个位数
            ll cnt = num/10 + (num%10 >= i);  //cnt表示以d结尾的、能整除m的数的个数
            sum += cnt*d;
        }
        cout << sum << endl;
    }
    return 0;
}