题目链接: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的更多相关文章

  1. 牛客练习赛13B 幸运数字2

    题目链接:https://ac.nowcoder.com/acm/contest/70/B 题目大意: 略 分析: 先DFS求出所有幸运数,然后暴力即可 代码如下: #pragma GCC optim ...

  2. 牛客练习赛13D:幸运数字Ⅳ(康托展开) F:关键字排序

    链接:https://www.nowcoder.com/acm/contest/70/D 题目: 定义一个数字为幸运数字当且仅当它的所有数位都是4或者7. 比如说,47.744.4都是幸运数字而5.1 ...

  3. 牛客练习赛13D

    定义一个数字为幸运数字当且仅当它的所有数位都是4或者7.比如说,47.744.4都是幸运数字而5.17.467都不是.现在想知道在1...n的第k小的排列(permutation,https://en ...

  4. 牛客提高D2t2 幸运数字考试

    分析 预处理出所有合法数字 然后直接lower_bound查询即可 代码 #include<iostream> #include<cstdio> #include<cst ...

  5. 牛客练习赛48 C 小w的糖果 (数学,多项式,差分)

    牛客练习赛48 C 小w的糖果 (数学,多项式) 链接:https://ac.nowcoder.com/acm/contest/923/C来源:牛客网 题目描述 小w和他的两位队友teito.toki ...

  6. 牛客练习赛48 A· 小w的a+b问题 (贪心,构造,二进制)

    牛客练习赛48 A· 小w的a+b问题 链接:https://ac.nowcoder.com/acm/contest/923/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...

  7. 牛客练习赛53 D 德育分博弈政治课 (思维建图,最大流)

    牛客练习赛53 D德育分博弈政治课 链接:https://ac.nowcoder.com/acm/contest/1114/D来源:牛客网 题目描述 德育分学长最近玩起了骰子.他玩的骰子不同,他的骰子 ...

  8. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  9. 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B

    牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...

随机推荐

  1. 《JAVA程序设计》_第一周学习总结

    20175217吴一凡 <java程序设计> 第一周学习总结 虽然已经做好了心理准备,但第一周的学习任务着实让我忙了整整三天,还是挺充实的吧.寒假已经在自己的电脑上安装好了虚拟机,我就在我 ...

  2. C# List去重的三种方法(转)

    三种去重的方法 1.List中的元素实现IEquatabe接口,并提供Equals方法和GetHashCode方法. 2.使用表达式 users.Where((x,i)=>users.FindI ...

  3. 爬取伯乐在线文章(二)通过xpath提取源文件中需要的内容

    爬取说明 以单个页面为例,如:http://blog.jobbole.com/110287/ 我们可以提取标题.日期.多少个评论.正文内容等 Xpath介绍 1. xpath简介 (1) xpath使 ...

  4. 吴恩达课后作业学习1-week3-homework-one-hidden-layer

    参考:https://blog.csdn.net/u013733326/article/details/79702148 希望大家直接到上面的网址去查看代码,下面是本人的笔记 建立一个带有隐藏层的神经 ...

  5. XSS高级利用

    XSS会话劫持 (1)Cookie简介 Cookie是能够让网站服务器把少量文本数据存储到客户端的硬盘.内存,或者是从客户端的硬盘.内存读取数据的一种技术. Cookie是一段随HTTP请求.响应一起 ...

  6. 使用 OAuth2-Server-php 搭建 OAuth2 Server

    Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...

  7. 我的微软最有价值专家(Microsoft MVP)之路

    一.写在前面 2018年对我来说是幸运的一年,对我来说最幸运的事情有两个,一个是在离驾照考试过期还有一个月(报名之后一直没去考)终于拿到了我的驾照,还有一件事莫过于获得了微软MVP.期间,一直有朋友问 ...

  8. 深入浅出Tomcat/2 - Tomcat启动和停止

    Tomcat启动和停止 很明显,我们启动或停止Tomcat,一般调用的是bin下的startup.sh或shutdown.sh(以Linux为例,以下涉及到平台,若无特殊说明,一般都指Linux).我 ...

  9. Ansible 简介

    Ansible 是一个开源的基于 OpenSSH 的自动化配置管理工具.可以用它来配置系统.部署软件和编排更高级的 IT 任务,比如持续部署或零停机更新.Ansible 的主要目标是简单和易用,并且它 ...

  10. [C# ASP.NET]如何让IIS Express支持外部(局域网)连接

    声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.搭建环境: 1.系统:Win10 1809 2.IDE:Visual Studio 2017 3.Framework: 4.6.1 ...