112123---123456
找个例题:
http://codeforces.com/contest/1328/problem/B
B. K-th Beautiful String
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For the given integer n (n>2) let’s write down all the strings of length n which contain n−2 letters ‘a’ and two letters ‘b’ in lexicographical (alphabetical) order.
Recall that the string s of length n is lexicographically less than string t of length n, if there exists such i (1≤i≤n), that si<ti, and for any j (1≤j<i) sj=tj. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.
For example, if n=5 the strings are (the order does matter):
aaabb
aabab
aabba
abaab
ababa
abbaa
baaab
baaba
babaa
bbaaa
It is easy to show that such a list of strings will contain exactly n⋅(n−1)2 strings.
You are given n (n>2) and k (1≤k≤n⋅(n−1)2). Print the k-th string from the list.
Input
The input contains one or more test cases.
The first line contains one integer t (1≤t≤104) — the number of test cases in the test. Then t test cases follow.
Each test case is written on the the separate line containing two integers n and k (3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2).
The sum of values n over all test cases in the test doesn’t exceed 105.
Output
For each test case print the k-th string from the list of all described above strings of length n. Strings in the list are sorted lexicographically (alphabetically).
Example
inputCopy
7
5 1
5 2
5 8
5 10
3 1
3 2
20 100
outputCopy
aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa
题目大意:
input: t,n,k
t 组数据, 字符串长度 n , 输出第k 个字符串字符串从尾部bb开始排列,主要是确定b的位置,设 pre, lat指向b,
i , j 分别为 pre,lat 的左移单位数初始 pre = n - 2 ,lat = n -1
题目转换为,寻找 i ,j 与 k 的关系
然后列个表看看:
i 0 1 1 2 2 2 3 3 3 3
j 0 0 1 0 1 2 0 1 2 3
s 1 2 3 4 (这一行表示每一个i 对应 排列种数)
k 1 2 3 4 5 6 7 8 9 10
i 与 s : s = i + 1
s 与 k: s(s+1)/2 (确定每一个边界对应)
j 与 k: k(n-1) + j + 1 = k;
通过第二个条件确定边界就基本解决了;
如下:
#include <iostream>
using namespace std;
typedef long long ll;void get_pre(int& pre,int& lat,const int n,const ll k)
{int i = 0, j = 0;for(int u = 0;u<n;++u){int s = u + 1;if((ll)s * (s + 1)/ 2 >= k){i = u ;break;}}//ll before= (ll)i * (i + 1) / 2;j = k - before - 1;//pre -= i;lat -= j;
}
//
int main()
{ios::sync_with_stdio(false), cin.tie(0);int t, n;ll k;while (cin >> t){while (t--){cin >> n >> k;int pre = n - 2, lat = n - 1;get_pre(pre, lat, n, k);for (int i = 0; i < n; ++i){if (i == pre || i == lat)cout << 'b';·elsecout << 'a';}cout << '\n';}}return 0;
}
好像也类似 112123.。。。
对
1121231234----1 2 3 4 5 6 7 8 9 10类: 寻找第k个数
1
1 2
1 2 3
....
1 2 3 4 5... n
第n行有 n个数,
1 2 3 4 5…等差数列 Sn = n(n+1)/2
通过遍历找到所在行
if(Sn>k) break; //此时得到大一个的n
所在行 即:n -1
则所在列:k - S(n-2)
所在列即为该数字。
112123---123456相关推荐
- php 挂马 密码123456,admin密码-常用密码加密md5值,123456,admin,admin888
一 : 常用密码加密md5值,123456,admin,admin888 123456这个经md5算法加密之后是多少? 16位:49ba59abbe56e057 32位:e10adc3949ba59a ...
- 不可思议!乌克兰国防军队的系统账密居然是 admin 和 123456...
编辑:可可.整理 开发者技术前线 2020年被用烂大街的密码,500 多万个泄漏密码表明,共有近 3% 的人使用"123456"作为密码.而最近知名黑客网站 Have I Been ...
- java---某人在玩游戏的时候输入密码123456后成功进入游戏(输错5次则被强行退出),要求用程序实现密码验证的过程。
题目: 某人在玩游戏的时候输入密码123456后成功进入游戏(输错5次则被强行退出),要求用程序实现密码验证的过程,要求如下: (1) 使用System.in包装为字符流读取键盘输入. (2) Buf ...
- mysql 123456_$myconn=mysql_connect(192.168.1.xxx,root,123456);怎么连不上数据库啊?
你的位置: 问答吧 -> PHP -> 问题详情 $myconn=mysql_connect("192.168.1.xxx","root",&quo ...
- 2020了,最流行的密码依旧是123456
喜欢就关注我们吧! 密码管理器 NordPass 于日前发布的一份报告中揭示了 2020 年最常见的密码,并提供了有关如何使密码更安全的建议. 根据 NordPass 的研究,在今年 200 种最常用 ...
- mysql远程连接权限grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘123456‘ with grant option语句报错
mysql远程连接权限grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option语句报错 记录 ...
- Javascript报错Failed to execute ‘querySelectorAll‘ on ‘Document‘: ‘#123456‘ is not a valid sele
Javascript报错:Failed to execute 'querySelectorAll' on 'Document': '#123456' is not a valid selector 解 ...
- mysql 123456_MySQL字符串中抽取数值的方法 select -(-'123456@163.com'); 很牛逼
MySQL的字符串函数非常多,以至于有时候我不知道该如何灵活的使用这些函数. 字符串基本信息函数 collation convert,char_length等 加密函数 password ...
- 正则表达式 判断 连号如“123456”、同号如“888888”、连同号如“112233”“222333”...
正则表达式 判断 连号如"123456".同号如"888888".连同号如"112233""222333" import ...
- net user test 123456 /add
net user test 123456 /add 转载于:https://www.cnblogs.com/seasonsstory/p/3432887.html
最新文章
- Tensorflow函数——tf.variable_scope()
- 面向对象课程第二单元作业总结
- BottomNavigationView+ViewPager+Fragment仿微信底部导航栏
- Windows 2000/XP/2003超级工具
- 你不得不知道的Visual Studio 2012(3)- 创建Windows应用程序
- linux系统判断是否重启、关机、查询登录诊断分析简介
- 如何在Ubuntu里安装Helm
- 【Manacher】绿绿和串串(luogu 5446)
- windows系统安装下GCC编译器
- Velodyne 16线三维激光雷达
- 第2章——R的数据组织
- Boost库编译指南
- 自学前端到上岸工作系列之css03
- 微信公众号头像如何修改
- 神通广大的WiFi劫持工具:Mana
- linux安装mysql8.11_Ubuntu安装MySQL任意版本(18.04亲测)
- python获取gps数据_Python GPS模块:读取最新的GPS数据
- rsa前后端加密流程_HTTPS 温故知新(三)——直观感受 TLS 握手流程(中)
- sdcms php版手册,目录结构及说明
- 两个mysql数据同步