Codeforces Round #587 (Div. 3) E
http://codeforces.com/contest/1216/problem/E1
E2. Numerical Sequence (hard version)
You are given an infinite sequence of form "112123123412345……" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 11 to 11, the second one — from 11 to 22, the third one — from 11 to 33, ……, the ii-th block consists of all numbers from 11 to ii.
So the first 5656 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 11-st element of the sequence is 11, the 33-rd element of the sequence is 22, the 2020-th element of the sequence is 55, the 3838-th element is 22, the 5656-th element of the sequence is 00.
Your task is to answer qq independent queries. In the ii-th query you are given one integer kiki. Calculate the digit at the position kiki of the sequence.
The first line of the input contains one integer qq (1≤q≤5001≤q≤500) — the number of queries.
The ii-th of the following qq lines contains one integer kiki (1≤ki≤109)(1≤ki≤10^18) — the description of the corresponding query.
Print qq lines. In the ii-th line print one digit xixi (0≤xi≤9)(0≤xi≤9) — the answer to the query ii, i.e. xixi should be equal to the element at the position kiki of the sequence.
5 1 3 20 38 56
1 2 5 2 0
4 2132 506 999999999 1000000000
8 2 9 8
Answers on queries from the first example are described in the problem statement.
思路:
1,可以先二分查找,寻找k在哪个数字的列中。
2,再进行一次二分查找,寻找k具体在哪一个数字。
3,在该数字中找到k。
有:
1,n的范围,假设每个数字的长度都为1(例如10,11长度为1),则(n+1)*n/2<=k;即n<√K。
2,长度为len的数字的个数有10^len-10^(len-1)。
可以确定一个数组sum[ i ],表示到i这个数(包括i)的总长度,str[ i ]表示第i个数的长度。例如:sum[ 5 ]=15,str[ 5 ]=5;
可以得出:
sum[ X(是10^i-1由特殊到一般) ] = sum[ 10^(i-1)-1 ] + i*cnt*(cnt+1)/2 + l * cnt;(i为当前数的位数,cnt为位数为i的数的个数,l 为 sum[ 10^(i-1)-1 ])
#include<iostream> #include<algorithm> #include<queue> #include<vector> #include<cstring> #include<cstdio> #include<cmath> #define ll long long using namespace std; ll gets(long long mm,int opt) { ll sum=0,cnt,add=0,len=0,exam=1; while(1) { exam*=10; len++; if(mm>exam-1) { cnt=exam-exam/10; if(opt) { sum+=add*cnt+cnt*(cnt+1)/2*len; add+=cnt*len; } else { sum+=cnt*len; } } else { cnt=mm-exam/10+1; if(opt) { sum+=add*cnt+cnt*(cnt+1)/2*len; add+=cnt*len; } else { sum+=cnt*len; } break; } } return sum; } int main() { int q; ll k; ll ans=0; scanf("%d",&q); while(q--) { ans=0; scanf("%lld",&k); ll l=0,r=1e9; while(l<=r) { ll mid=(l+r)>>1; if(gets(mid,1)<k) { l=mid+1; ans=mid; } else { r=mid-1; } } k-=gets(ans,1); l=0; r=ans+1; while(l<=r) { ll mid=(l+r)>>1; if(gets(mid,0)<k) { l=mid+1; ans=mid; } else { r=mid-1; } } k-=gets(ans,0); ans+=1; //printf("ans=%lld,k=%lld\n",ans,k); cout<<to_string(ans)[k-1]<<endl; } }
原文地址:https://www.cnblogs.com/switch-waht/p/11567568.html
- Linq基础知识小记二
- IntelliJ IDEA 中的版本控制介绍(中)
- React Native动画详解
- 小程序实现原理解析
- EF 通过DataAnnotations配置属性和类型
- IntelliJ IDEA 中的版本控制介绍(上)
- EF基础知识小记七(拆分实体到多个表以及拆分表到多个实体)
- Android 进程回收之LowMemoryKiller原理篇
- 邮件发送功能开发
- Vuejs和其他前端框架的对比
- 详述 IntelliJ IDEA 中恢复代码的方法
- C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能
- 微信小程序之picker组件
- 详述 IntelliJ IDEA 中恢复代码的方法「进阶篇」
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 让终端支持https,移植OpenSSL和libcurl到嵌入式linux,遇到的问题总结
- ComplexHeatmap|根据excel表绘制突变景观图(oncoplot)
- R-plotly|甘特图(Gantt chart)- 一不小心年中了,立个flag
- XXE漏洞那些事儿(JAVA)
- Hexo 建站过程
- Tidyverse|tidyr数据重塑之gather,spread(长数据宽数据转化)
- fastjson中的jndi注入
- R|生存分析-结果整理
- 实验吧-因缺思汀的绕过
- MySQL 用户与授权管理详解
- 基本知识|R语言简单饼图的绘制
- 分享一个有趣且牛逼的漏洞
- ggplot2-annotation|画图点“精”,让图自己“解释”
- 使用curl工具调试https接口
- 让android支持https访问银联后台,测试成功