题目描述:

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

Output

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

大致题意:

输入m,n两个数,求出1-m中能整除n的数的尾数之和

解题思路:

若直接遍历1-m或者每次以n的倍数递增循环都会造成时间超限,其实有一个简单的规律,一个数的倍数的尾数可以形成一个循环节,假如n为7,循环节则为7,1,若为3,则循环节为3,9,7,1,只要求出有多少个循环节就可以了

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int t,vis[20],vis1[20];
long long int m,n;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        if(n>m)
            cout<<0<<endl;
        else
        {
            memset(vis,0,sizeof(vis));
            long long int num1=m/n;
            int flag=0;
            long long int flag1=0;
            for(int i=1; i<=num1; i++)
            {
                int num2=(n*i)%10;
                vis1[i]=num2;
                vis[num2]++;
                if(vis[num2]==2)
                    break;
                flag1+=num2;
                flag++;
            }
            flag1=(num1/flag)*flag1;
            if(num1%flag!=0)
            {
                for(int i=1; i<=num1%flag; i++)
                {
                    flag1+=vis1[i];
                }
            }
            cout<<flag1<<endl;
        }
    }
    return 0;
}