Little Sub and Mr.Potato's Math Problem (构造法)
题目传送门Little Sub and Mr.Potato's Math Problem
Time Limit: 2 Seconds Memory Limit: 65536 KB
Little Sub loves math very much. He enjoys counting numbers.
One day, Mr.Potato gives him an interesting math problem. Please help Little Sub solve this problem.
Let's sort the integers according to alphabetical order. For example, when , the order should be: .
We define as the position of number in the sorted numbers. For example, .
Given and , please find the smallest such that .
Input
There are multiple test cases. The first line of the input contains an integer (), indicating the number of test cases. For each test case:
The first and only line contains two integers and (, ).
Output
For each test case, please output the answer in one line. If there is no such , please output "0" (without quotes).
Sample Input
2
2 4
10000001 100000000
Sample Output
11
1000000088888880 题意:如上题 思路:
第一步:我们可以先计算出当前的数前面按照字典序的话,前面有多少数(包括自己)。
(具体计算过程,456.首先是三位数的数在456前面的个数,就是(100->456),然后再就是两位数(10-45),然后再就是一位数1-4,严格遵守字典序)。
第二步:如果说当前的数前面的数的个数大于m的话,这个时候肯定是不符合情况的,如果说刚好凑起来的话,这个时候输出k就可以了。
第三步:经过了第二步,发现当前的数N取k的时候凑不起来,我们就需要通过增加n来使得k的位数往后移动。举个例子,456不够,我们就可以通过添加1000-4560之间的数来使得456的位数往后移动,如果还不够我们就添加10000-45600之间的数使得456的位数往后移动。
第四步:我们需要判断一种特殊情况,比如说 10 10这个样例,10前面的数只有1,但是你如果加100之后的,也不会使得10的位数往后移动,这个时候就应该输出0。(就是这些数的字典序是相对固定的)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
int k,m;
ll base=; ll get_num(ll x)
{
ll ans=;
ll tmp=x;
while(x){
base*=;
x/=;
}
base/=;
ll base1=base;
while(tmp)
{
ans+=(tmp-base1)+;
tmp/=;
base1/=;
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&k,&m);
base=;
ll tmp=get_num(k);
m-=tmp;
if(m<) printf("0\n");
else if(m==) printf("%d\n",k);
else if(base==k) printf("0\n");
else{
ll ans=k-base;
base*=;
ans*=;
while(m>ans)
{
m-=ans;
ans*=;
base*=;
}
printf("%lld\n",base+m-);
}
}
return ;
}
参考博客:https://www.cnblogs.com/letlifestop/p/10294237.html
Little Sub and Mr.Potato's Math Problem (构造法)的更多相关文章
- (P2022 有趣的数)||(zoj Little Sub and Mr.Potato's Math Problem)(思维)
题目链接:https://www.luogu.org/problemnew/show/P2022 题目大意:中文题目 具体思路: 第一步:我们可以先计算出当前的数前面按照字典序的话,前面有多少数(包括 ...
- Little Sub and Mr.Potato's Math Problem-构造
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5864 思路 : 判断小于它的合法的,再看大于它的合法的,特判10000. ...
- hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)
题意:有一个递推式f(x) 当 x < 10 f(x) = x.当 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 5615 Jam's math problem
Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...
- hdu----(5055)Bob and math problem(贪心)
Bob and math problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- BestCoder Round #70 Jam's math problem(hdu 5615)
Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ...
随机推荐
- 使用Jmeter聚合报告生成对比图表
背景 最近在帮别的项目组执行性能测试,使用的工具是Jmeter.接口录制和参数化前一个人已经做好了,我主要的工作就是执行脚本,撰写测试报告.事情并不复杂,可做起来却极为耗时. 首先,由于有6组账号,分 ...
- HDU-6038 Function 思维+循环节
解法:这道题很有意思,值得一做和细细思考. 首先是我们要观察这个映射公式,他的实质是什么?其实就是b到a的循环映射,这是因为a数列和b数列都是0-n-1的排列,意味着每个数都是唯一的,那么ab的这个循 ...
- SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】
题目链接 一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分 #include<bits/stdc ...
- swan.after
解释: swan.after可以拦截所有当前运行小程序对于API的调用,默认传入function时,只在API函数调用的返回阶段拦截.如果传入Object,则可以选择拦截的阶段(例如: 返回阶段.回调 ...
- Delphi 清理程序内存
procedure ClearMemory;begin if Win32Platform = VER_PLATFORM_WIN32_NT then begin ...
- 51nod1446 限制价值树
有N个点(N<=40)标记为0,1,2,...N-1,每个点i有个价值val[i],如果val[i]=-1那么这个点被定义为bad,否则如果val[i] >=0那么这个点为定义为good. ...
- Java实践-远程调用Shell脚本并获取输出信息
1.添加依赖 <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-s ...
- centos 无界面安装selenium+chrome+chromedirver的设置
配了一中午的,好不容易正好记录下. 1.我的centos的位数 输入rpm -q centos-release 结果:centos-release-7-4.1708.el7.centos.x86_64 ...
- pycharm默认的模板修改python script
#!/usr/bin/env python # encoding: utf-8 #set( $SITE = "https://www.cnblogs.com/c-x-a" ) &q ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...