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

 

全部评论

相关推荐

2025年10月3日中午,在写完定时一年后发给自己的信之后,敲下键盘,写下这篇文字。我把标题的“所有人”加了引号,因为如我们所见,确实有的人顺风顺水,每天过的很开心,或是早早进入大厂,或是年纪轻轻就拿到了高薪offer,或是过着可能我努力十年也不一定实现的生活。但也许,不是每个人的痛苦都能被别人看到的,这个月我经常会哭,被骗6000块钱、手上钱不够导致拖欠房租、生活还要借朋友钱、国庆长假也没有钱去旅游,互联网公司不稳定担心试用期不过(毕竟上段实习就是被裁了,一有点风吹草动就害怕),但这样的我,不是所有人都知道的,居然是有些朋友的羡慕对象。回忆我的七年“长跑”别人都是多年幸福的恋爱长跑,我没有恋...
故事和酒66:让每一颗种子找到合适自己的生长方式,最终绽放出独一无二的花朵,这远比所有人都被迫长成同一棵“参天大树”的世界,更加美好和富有生机。这是社会和环境的问题,而不是我们的问题。然而就是在这样的环境中,楼主依然能突破自我,逆势成长,其中的艰辛可想而知。这一路的苦难终究会化作你成长的养料
你小时候最想从事什么职业
点赞 评论 收藏
分享
用微笑面对困难:这里面最强的是驾驶证了,可以入职美团大厂,然后直接开启黄马褂人生
点赞 评论 收藏
分享
青州街志明:bro吃肉,给兄弟们喝口汤呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务