Description

AK:All kill
“你为什么没背书?”
“没有为什么,我就是没背书。”
“……我去年买了个表,G—U—N!”
头铁王InFleaKing把背书的时间都拿去列排列了......
n=3的排列一共有六个(顺序按字典序从小到大):
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
气不打一处来的InFleaKing把n的排列打乱了。
他想知道字典序第k小的n的排列是什么?
由于InFleaKing被捉去背书了,所以这个问题只能交给被万人顶礼膜拜的dalao您来解决了。
 

Input

一行,两个数,分别是n,k。
n,k的含义见题目描述。

Output

一行,n个数。
代表字典序第k小的n的排列。
注意两两数之间要用空格隔开。
 

Sample Input

Sample Input1:
1 1 Sample Input2:
2 2
 

Sample Output

Sample Output1:
1 Sample Output2:
2 1
 
 

Data Constraint

【数据约定】
对于10%的数据:1<=n<=3
对于20%的数据,1<=n<=9
对于30%的数据:1<=n<=18,1<=k<=10^6
对于60%的数据:1<=n<=18
对于80%的数据:1<=n<=100,1<=k<=10^100
对于90%的数据:1<=n<=1000,1<=k<=10^1000
对于100%的数据:1<=n<=100000,1<=k<=min(10^20000,n!)

直接模拟...不知道为什么讲的这么玄学。

其实是显然的事。

对于从左到右的位置i,前面有(n-1)!种可能...

于是就把剩下的数字中第k / (n - 1)!个数选到当前位置, 然后k %= (n - 1)!,

然后对于后面也是这样。。。

要写高精不开心,直接弃疗,手懒,我是不会写高精的233.

复杂度是O(N^2)的过不去,听讲解的什么奇怪算法不太明白为什么要这么做。

直接用树状数组维护, 再套二分找第k个数就ok了。

复杂度O(Nlog^2N)。

考场上O(N^2)能过不写高精的数据,于是弃疗了没写树状数组套二分。

放上考场60分代码


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
inline int read() {
int res=;char c=getchar();bool f=;
while(!isdigit(c)) {if(c=='-')f=;c=getchar();}
while(isdigit(c))res=(res<<)+(res<<)+(c^),c=getchar();
return f?-res:res;
}
#define ll long long
int n;
unsigned ll k;
int wei[];
unsigned ll fac[];
bool vis[]; int main()
{
freopen("array.in", "r", stdin);
freopen("array.out", "w", stdout);
n = read();
fac[] = ;
for (int i = ; i <= n ; i ++) fac[i] = fac[i-] * i;
cin >> k;
k--;
for (register int i = ; i < n ; i ++)
{
int re,sum = ;
for (register int j = ; j <= n; j ++)
{
if (sum == k / fac[n-i] + ) break;
if (!vis[j]) re = j, sum++;
}
printf("%d ", re);
vis[re] = ;
k %= fac[n-i];
}
for (register int i = ; i <= n ; i ++) if (!vis[i]) return printf("%d", i), ;
return ;
}

[JZOJ5772]【NOIP2008模拟】今天你AK了吗?的更多相关文章

  1. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  2. JZOJ 5809. 【NOIP2008模拟】数羊

    5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms  Memory Limits: ...

  3. JZOJ 5793. 【NOIP2008模拟】小S练跑步

    5793. [NOIP2008模拟]小S练跑步 (File IO): input:run.in output:run.out Time Limits: 2000 ms  Memory Limits:  ...

  4. JZOJ 5791. 【NOIP2008模拟】阶乘

    5791. [NOIP2008模拟]阶乘 (File IO): input:factorial.in output:factorial.out Time Limits: 1000 ms  Memory ...

  5. JZOJ 5776. 【NOIP2008模拟】小x游世界树

    5776. [NOIP2008模拟]小x游世界树 (File IO): input:yggdrasil.in output:yggdrasil.out Time Limits: 1500 ms  Me ...

  6. JZOJ 5775. 【NOIP2008模拟】农夫约的假期

    5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms  Memory Lim ...

  7. JZOJ 5773. 【NOIP2008模拟】简单数学题

    5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms  Memory Limits ...

  8. JZOJ 5771. 【NOIP2008模拟】遨游

    5771. [NOIP2008模拟]遨游 (File IO): input:trip.in output:trip.out Time Limits: 2000 ms  Memory Limits: 2 ...

  9. JZOJ5776. 【NOIP2008模拟】小x游世界树

    题目:[NOIP2008模拟]小x游世界树: 题目的附加题解给的很清楚,这里只给一个代码: #include<iostream> #include<cstdio> #inclu ...

随机推荐

  1. 重学Java(一):与《Java编程思想》的不解之缘

    说起来非常惭愧,我在 2008 年的时候就接触了 Java,但一直到现在(2018 年 10 月 10 日),基础知识依然非常薄弱.用一句话自嘲就是:十年 IT 老兵,Java 菜鸡一枚. 于是,我想 ...

  2. P0.0口驱动一个LED闪烁

    #include<reg51.h> //头文件 sbit LED=P0^; //led接P0.0,定义P0.0为P0^0 void delay(unsigned int x) //延时函数 ...

  3. 使用git管理github上的代码

    第一次接触git是使用git来提交自己的github的代码,在new repository之后,github会给出一些操作示例. 示例如下: …or create a new repository o ...

  4. KeyValue对RDDs

    创建KeyValue对RDDs 使用函数map,返回keyvalue对 例如,包含数行数据的RDD,那每行的第一个单词作为keys. sparl.txt中的内容如下: hello ! hello wo ...

  5. 小记redis持久化的机制

    刚学redis,就经常看到两种持久化机制在眼头晃,RDB和AOF,然而当时学的还知道这两东西是啥玩意,过段时间又忘了,中文记忆这两种概念总感觉有些别扭.今心血来潮翻看redis的配置文件,豁然开朗,仿 ...

  6. SpringBootSecurity学习(09)网页版登录配置Session共享

    场景 当后台项目由部署在一台改为部署在多台以后,解决session共享问题最常用的办法就是把session存储在redis等缓存中.关于session和cookie概念这里就不再赘述了,在spring ...

  7. Spring 梳理-使用<context:property-placeholder>标签导入多个properties文件

    使用<context:property-placeholder>标签导入多个properties文件 2017年12月20日 10:10:36 sf_climber 阅读数:5830更多 ...

  8. mybatis 配置之<typeAliases>别名配置元素设置

    一.方式一:使用typeAlias <typeAliases> <typeAlias alias="User" type="com.**.entity. ...

  9. Spring 中使用 @Scheduled 创建定时任务

    一.定时任务触发条件 1.在 Application 启动类上添加:@EnableScheduling 2.含定时方法的类上添加注解:@Component,该注解将定时任务类纳入 spring bea ...

  10. 在Linux系统下有一个目录/usr/share/dict/ 这个目录里包含了一个词典的文本文件,我们可以利用这个文件来辨别单词是否为词典中的单词。

    #!/bin/bash s=`cat /usr/share/dict/linux.words` for i in $s; do if [ $1 = $i ];then echo "$1 在字 ...