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.

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值