Gym-100923L-Por Costel and the Semipalindromes(进制转换,数学)
链接:
https://vjudge.net/problem/Gym-100923L
题意:
Por Costel the pig, our programmer in-training, has recently returned from the Petrozaporksk training camp. There, he learned a lot of things: how to boil a cob, how to scratch his belly using his keyboard, etc... He almost remembers a programming problem too:
A semipalindrome is a word for which there exists a subword such that is a prefix of and (reverse ) is a suffix of . For example, 'ababba' is a semipalindrom because the subword 'ab' is prefix of 'ababba' and 'ba' is suffix of 'ababba'.
Let's consider only semipalindromes that contain letters 'a' and 'b'. You have to find the -th lexicographical semipalindrome of length .
Por Costel doesn't remember if the statement was exactly like this at Petrozaporksk, but he finds this problem interesting enough and needs your help to solve it.
思路:
考虑首字母等于末字母,中间按字典序处理即可,字典序处理可用二进制的思想.
aaa = 000, aab = 001, aba == 010, 就是挨个进位,注意,第k小在字典序的二进制中是k-1, 因为aaa = 000,从0开始.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
map<string, double> Sc;
const int MAXN = 1e3+10;
char s[100];
LL n, k;
//aaaa, aaba, abaa, abba, baba,
int main()
{
freopen("semipal.in", "r", stdin);
freopen("semipal.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n >> k;
if (k > 1LL<<(n-2))
{
k -= 1LL<<(n-2);
k--;
s[0] = 'b';
s[n-1] = 'b';
for (int i = n-2;i >= 1;i--)
{
if (k%2 == 0)
s[i] = 'a';
else
s[i] = 'b';
k /= 2;
}
}
else
{
s[0] = 'a';
s[n-1] = 'a';
k--;
for (int i = n-2;i >= 1;i--)
{
if (k%2 == 0)
s[i] = 'a';
else
s[i] = 'b';
k /= 2;
}
}
s[n] = 0;
cout << s << endl;
}
return 0;
}
Gym-100923L-Por Costel and the Semipalindromes(进制转换,数学)的更多相关文章
- 【找规律】Gym - 100923L - Por Costel and the Semipalindromes
semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...
- SQL Server 进制转换函数
一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...
- [No000071]C# 进制转换(二进制、十六进制、十进制互转)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- JS中的进制转换以及作用
js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...
- 结合stack数据结构,实现不同进制转换的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 进制转换( C++字符数组 )
注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...
- php的进制转换
学习了php的进制转换,有很多的知识点,逻辑,也有最原始的笔算,但是我们还是习惯使用代码来实现进制的转换,进制的转换代码有如下:二进制(bin)八进制( oct)十进制( dec)十六进制( hex) ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
随机推荐
- Java学习之==>int和Integer的区别和联系
一.区别 1.类型 int是java中原始八种基本数据类型之一: Integer是一个类,包装整型提供了很多日常的操作: 2.存储位置和大小 int是由jvm底层提供,由Java虚拟机规范,int型数 ...
- 如图 honehoneclock
如图人体时钟 honehoneclock 页面演示来自http://chabudai.org/blog/?p=59 <embed name="honehoneclock" ...
- SSM的开发步骤分析
完整开发步骤 导包 spring的jar包 mybatis的jar包 mybatis-Spring的jar包 aop的依赖jar包 oracle等数据库连接的jar包 DataSource的jar包 ...
- SpringBoot常用注解有哪些?
@Service: 注解在类上,表示这是一个业务层bean@Controller: 注解在类上,表示这是一个控制层bean@Repository: 注解在类上,表示这是一个数据访问层bean@Comp ...
- json,异步加载,时间线
JSON是一种传输数据的格式 JSON.stringify(obj); obj--string JSON.parse(str); string-->obj
- USACO刷题索引
序 在距离CSP2019还有41天的国庆备战中,考了一场画风非常奇特的六校联赛,然后被教练建议刷一下这个巩固代码实现能力,然后就来了||ヽ(* ̄▽ ̄*)ノミ|Ю. 这个网站还是挺好玩儿的吧,刚开始各种 ...
- Mac022-brew安装tool
1.Mac上查看目录的树结构 Step1:安装tree命令 brew install tree Step2:某一目录下执行tree,会将该目录下的所有目录以树形结构显示 $ tree $ tree - ...
- jinja2模板接受
from flask import Flask,render_template app = Flask(__name__)#template_folder='templates',默认就是templa ...
- 详解Vue 如何监听Array的变化
详解Vue 如何监听Array的变化:https://www.jb51.net/article/162584.htm
- 洛谷 P1233 木棍加工 题解
题面 Dilworth定理:在数学理论中的序理论与组合数学中,Dilworth定理根据序列划分的最小数量的链描述了任何有限偏序集的宽度. 反链是一种偏序集,其任意两个元素不可比:而链则是一种任意两个元 ...