C. Book Reading 求在[1,n]中的数中,能整除m的数 的个位的和
C. Book Reading
1 second
256 megabytes
standard input
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.
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.
For each query print the answer for it — the sum of digits written down by Polycarp.
7
1 1
10 1
100 3
1024 14
998244353 1337
123 144
1234312817382646 13
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[][]={ //vis[i][0] 指m的个位数为i的时候循环节长度为vis[i][0],vis[i][j] (j>=1)是循环内的每一个数
{,},
{,,,,,,,,,,},
{,,,,,},
{,,,,,,,,,,},
{,,,,,},
{,,},
{,,,,,},
{,,,,,,,,,,},
{,,,,,},
{,,,,,,,,,,}
};
int sum[];
int main()
{
ll t, n, m, ans;
cin >> t;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
sum[i]=sum[i]+vis[i][j];
} while (t--)
{
ans = ;
cin >> n >> m;
ll k=n/m;
ll x=m%;
ans=ans+sum[x]*(k/vis[x][]);
k=k%vis[x][];
for(int i=;i<=k;i++)//不够一个循环的和
ans=ans+vis[x][i];
cout<<ans<<endl;
}
return ;
}
C. Book Reading 求在[1,n]中的数中,能整除m的数 的个位的和的更多相关文章
- js如何求一组数中的极值
这是一个很简单的问题,现在我们从循环开始,例如一组数[5,2,1,3,4];求其中的最大值,那么首先我们要定义一个max的中间变量,遍历数组,当遇到比max值大则赋值给max,直到循环结束,就能获取这 ...
- 【C语言】输入一组整数,求出这组数字子序列和中最大值
//输入一组整数.求出这组数字子序列和中最大值 #include <stdio.h> int MAxSum(int arr[],int len) { int maxsum = 0; int ...
- Jsの数组练习-求一组数中的最大值和最小值,以及所在位置
要求:求一组数中的最大值和最小值,以及所在位置 代码实现: <!DOCTYPE html> <html lang="en"> <head> &l ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- 算法题:给你一个自然数N,求[6, N]之内的全部素数中, 两两之和为偶数的那些偶数。
/* 算法题:给你一个自然数N,求[6, N]之内的全部素数中. 两两之和为偶数的那些偶数. */ #include <iostream> using namespace std; voi ...
- Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)
题意 题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上.题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青 ...
- lua中 table 元表中元方法的重构实现
转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...
- 欧拉函数(小于或等于n的数中与n互质的数的数目)&& 欧拉函数线性筛法
[欧拉函数] 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler’s totient function.φ函数.欧拉商数等. 例如φ( ...
- 给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数。
题目:给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数. 解题思路: 首先取得数组a的中位数a[aMid],然后在b中二分查找a[aMid],得到b[bMid],b[bSt] ...
随机推荐
- 好用的px转rem插件cssrem
下载本项目,比如:git clone https://github.com/flashlizi/cssrem 进入packages目录:Sublime Text -> Preferences - ...
- 传奇GOM引擎授权过期解决方法.
传奇GOM引擎授权过期解决方法 下载最新的GOM引擎,将里面的Key.Lic文件找出来,替换掉授权过期的版本,如果你本身是免费版最好是找同样的免费版的来覆盖. 如果你本身是免费版,但是却用的是商业版K ...
- tp5 配置 // 视图输出字符串内容替换 'view_replace_str' 的原理
- Ubuntu Rabbitmq 安装与配置
原文链接:http://blog.csdn.net/rickey17/article/details/72756766 添加源 新增公钥(不加会有警告) 更新源 安装rabbitmq-server e ...
- acm数论之旅(转载)--素数
https://www.cnblogs.com/linyujun/p/5198832.html 前言:好多学ACM的人都在问我数论的知识(其实我本人分不清数学和数论有什么区别,反正以后有关数学的知识我 ...
- 20141110的alltosun面试
今天周一,是校招的第一天,心情有点紧张,不过可以和很多同学一起去,是我紧张的心情变得稍微安静些.面试进行的时候,是学长2哥面的我,总体感觉自己的表现很糟糕,在公共场合发表言论或者演讲,一直是我的一个弱 ...
- windows远程linux的方法(不用xshell)
先cmd进入DOS,再输入命令ssh root@要远程的linux的ip 输入密码 即可进入linux后台.如下图,即为edr后台,可以见到unabackup服务了. 如果是多次远程不同IP,第二次远 ...
- selenium 元素查找与属性
1.首先你要安装selenium库啦 pip install selenium 2.selenium查找元素就八种方法 from selenium import webdriver driver=we ...
- 【知识学习】PHP实现批量替换字典后缀
<?php //要打开字典的物理路径 $filename = 'E:\Local Test\WWW\password.txt'; $handle = fopen($filename,'r') o ...
- es 搜索功能简介
DSL 语法介绍 语法 范围 /_search 集群上搜索所有的索引 /index1/_search index1 /index1,index2/_search index1和index2 /inde ...