C. Book Reading 求在[1,n]中的数中,能整除m的数 的个位的和

C. 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 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.

Input

The first line of the input contains one integer qq (1q10001≤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 (1n,m10161≤n,m≤1016) — the number of pages in the book and required divisor, respectively.

Output

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

Example
input
Copy
7
1 1
10 1
100 3
1024 14
998244353 1337
123 144
1234312817382646 13
output
Copy
1
45
153
294
3359835
0
427262129093995

题意:输入n,m; 求在[1,n]中的数中,能整除m的数 的个位的和

题解:规律+模拟

例如:n=100    m=3

1-n能被3整除的有:3 6 9 12 15 18 21 24 27 30 33 36 ……99

余数分别为:      3 6 9 2 5 8 1 4 7 0 3 6 …… 9

发现循环节的个数有:10个数   且和为3 + 6+9+2+5+8+1+4+7+0 = 45

所以最后的结果为:(n/m/循环节的个数) *   一个循环的和 +  不够一个循环节的和

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
int vis[10][12]={   //vis[i][0]  指m的个位数为i的时候循环节长度为vis[i][0],vis[i][j] (j>=1)是循环内的每一个数
    {1,0},
    {10,1,2,3,4,5,6,7,8,9,0},
    {5,2,4,6,8,0},
    {10,3,6,9,2,5,8,1,4,7,0},
    {5,4,8,2,6,0},
    {2,5,0},
    {5,6,2,8,4,0},
    {10,7,4,1,8,5,2,9,6,3,0},
    {5,8,6,4,2,0},
    {10,9,8,7,6,5,4,3,2,1,0}
};
int sum[10];
int main()
{
    ll t, n, m, ans;
    cin >> t;
    for(int i=0;i<10;i++)
    {
        for(int j=1;j<12;j++)
            sum[i]=sum[i]+vis[i][j];
    }
    
    while (t--)
    {
        ans = 0;
        cin >> n >> m;
        ll k=n/m;
        ll x=m%10;
        ans=ans+sum[x]*(k/vis[x][0]);
        k=k%vis[x][0];
        for(int i=1;i<=k;i++)//不够一个循环的和
            ans=ans+vis[x][i];
        cout<<ans<<endl;
    }
    return 0;
}


原文地址:https://www.cnblogs.com/-citywall123/p/11599335.html