牛客练习赛13D 幸运数字4
题目链接:https://ac.nowcoder.com/acm/contest/70/D
题目大意:
略
分析:
注意到12! < 10^9 < 13!,于是当n > 13时,第k号排列的前n - 13位是确定的。比如n = 15吧,那么无论k取何值,第k号排列都是形如:“12xxxxxxxxxxxxx”,后面的x代表其他数字,因为后面13个x,有13!种排列,大于10^9。于是平移一下就退化成n = 13的情况了,同时不要忘了把前n - 13位种满足条件的幸运数算一下。
对于n <= 13的情况,如果直接从第一个排列生成到第k个排列势必会超时,这里就要用到康托展开求出排列,然后遍历一遍即可。
关于康托展开是什么,看大佬链接:http://www.cnblogs.com/linyujun/p/5205760.html
代码如下:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef set< int > SI;
typedef vector< int > VI;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ; // 12! < 1e9 < 13!
LL n, k, ans;
LL b; // 偏移 LL lucky[], len;
LL fac[] = {, , , , , , , , , , , , , , , , , , , , }; /**
* @brief 康托展开,把数字转换成排列
* @param A 所要求的从1~k的一个排列
* @param num 排列A对应的数字,范围从0~k!-1
* @param k 所要求的的排列长度
*
* @return 无
*/
void cantor(int A[], LL num, int k) {
int t;
bool vis[k];//0到k-1,表示是否出现过
ms0(vis);
Rep(i, k){
t = num / fac[k - i - ];
num = num % fac[k - i - ];
// 找出尚未被标记过的第t大的数
for(int j = , pos = ; ; ++j, ++pos){
if(vis[pos]) --j;
if(j == t){
vis[pos] = true;
A[i] = pos + ; // 排列是从1开始的,所以要记得加1
break;
}
}
}
} /**
* @brief 康托逆展开,把排列转换成数字
* @param A 从1~k的一个排列
* @param num 排列A对应的数字,范围从0~k!-1
* @param k 排列长度
*
* @return 无
*/
void inv_cantor(int A[], LL &num, int k) {
int cnt;
num = ;
Rep(i, k) {
cnt = ;
// 判断A[i]后面有几个数小于它
For(j, i + , k - ) if(A[i] > A[j]) cnt ++;//判断几个数小于它
num += fac[k - i - ] * cnt;
}
} // 求所有幸运数
inline void dfs(LL x, int cnt) {
if(cnt > ) return;
lucky[len++] = x;
dfs(x* + , cnt + );
dfs(x* + , cnt + );
} bool check(LL x) {
int tmp = find(lucky, lucky + len, x) - lucky;
return tmp < len;
} int main(){
INIT();
cin >> n >> k;
dfs(, );
sort(lucky, lucky + len); if(n > ) {
b = n - ;
n = ;
ans = upper_bound(lucky, lucky + len, b) - lucky;
--ans;
}
if(k > fac[n]) {
cout << - << endl;
return ;
} //利用康托展开计算排列序列
int s[];
cantor(s, k - , n); Rep(i, n) if(check(i + b + ) && check(s[i] + b)) ++ans; cout << ans << endl;
return ;
}
牛客练习赛13D 幸运数字4的更多相关文章
- 牛客练习赛13B 幸运数字2
题目链接:https://ac.nowcoder.com/acm/contest/70/B 题目大意: 略 分析: 先DFS求出所有幸运数,然后暴力即可 代码如下: #pragma GCC optim ...
- 牛客练习赛13D:幸运数字Ⅳ(康托展开) F:关键字排序
链接:https://www.nowcoder.com/acm/contest/70/D 题目: 定义一个数字为幸运数字当且仅当它的所有数位都是4或者7. 比如说,47.744.4都是幸运数字而5.1 ...
- 牛客练习赛13D
定义一个数字为幸运数字当且仅当它的所有数位都是4或者7.比如说,47.744.4都是幸运数字而5.17.467都不是.现在想知道在1...n的第k小的排列(permutation,https://en ...
- 牛客提高D2t2 幸运数字考试
分析 预处理出所有合法数字 然后直接lower_bound查询即可 代码 #include<iostream> #include<cstdio> #include<cst ...
- 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)
牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...
- 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)
牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...
- 牛客练习赛53 D 德育分博弈政治课 (思维建图,最大流)
牛客练习赛53 D德育分博弈政治课 链接:https://ac.nowcoder.com/acm/contest/1114/D来源:牛客网 题目描述 德育分学长最近玩起了骰子.他玩的骰子不同,他的骰子 ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B
牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...
随机推荐
- Linux最终将会领先于Windows、Mac OS!
Linux最终将会领先于Windows.Mac OS! 众所周知,硬件离开了软件就像人失去灵魂,而操作系统作为软件中的基础部分,更是重中之重!无论手机.电脑.还是服务器一旦离开了操作系统,比之一堆废铁 ...
- python六十八课——网络编程之UDP协议
1.1 概述 UDP是无连接通信协议,即在数据传输时,数据的发送端和接收端不建立逻辑连接.简单来说,当一台计算机向另外一台计算机发送数据时,发送端不会确认接收端是否存在,就会发出数据,同样接收端在收到 ...
- 什么是数据库ACID?
原子性:由于操作失败导致的数据碎片错误: 一致性:由于并发导致的数据库数据错误(与预期不一致): 隔离性:由于并发导致的当前使用数据(应用端)错误: 事务在当今的企业系统无处不在,即使在高并发环境下也 ...
- 【适合N卡独显电脑的环境配置】Tensorflow教程-Windows 10下安装tensorflow 1.5.0 GPU with Anaconda
注意: 1.目前Anaconda 更新原命令activate tensorflow 改为 conda activate tensorflow 2. 目前windows with anaconda 可以 ...
- 发现一款比echarts更牛B,效果更炫的图表组件 d3.js
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code d3.js ,能制作更加复杂的图表 https://github.com/d3/d3 ...
- 002_浅析python 中__name__ = '__main__' 的作用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- day25 Python
一.day24复习 class school: x=1 #__init__初始化函数,用来帮类实例化一个具体的对象 def __init__(self,name,addr): #前面的Name是一个需 ...
- C语言初学者关于数组指针的深度讨论
一.什么是数组指针? 即是数组的指针.首先它是一个指针,指向数组,指针本身占4个字节. 二.数组指针的使用 int a[3][5]; int (*p)[5]; p=&a; 第二行定义了一个数组 ...
- sklearn官网-多分类问题
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- 不可变对象和Biulder模式(面试问题)
String就是一个典型的不可变对象.外界的操作不能改变它,如果尝试改变都会返回一个新的String对象. 具体实现起来就是把属性全部变成private 和 final的,这个类也是final的不可继 ...