CF285D.D. Permutation Sum
CF285D. Permutation Sum
题目
大意
寻找a,b两个排列从0到n-1,有c[i]=(a[i]+b[i])%n+1,使得c[i]也为全排列的排列方式
思路
a中元素和b中元素的对应方式不同,c数组也不同,且a和b此时全排列方式各有n!种。
可以先固定a中的数,从0到n-1,再dfs搜索b与之相应匹配的值,当然时间消耗会很大(求解n=15大概在五分钟左右)
再看题目条件,n最大到16,所以可以考虑打表方法,获取全部的值。
求解出来的答案再考虑a数组本身的全排列,且b随之对应排列,故需乘上n!
代码
首先打表:
#include<iostream>
#include<cstring>
using namespace std;
int a[20],b[20];
int sign[20];
int count;
void dfs(int now, int num) {
if(now == num) {
count++;
return;
}
for(int i=0;i<num;i++) {
if(b[i]) { // b[i]已经使用过,忽略
continue;
}
int c = (a[now]+i)%num+1;
if(sign[c]) { // c不构成全排列,忽略
continue;
}
sign[c]=1;
b[i]=1;
dfs(now+1, num);
sign[c]=0;
b[i]=0;
}
}
int main() {
for(int i=0;i<16;i++) {
a[i] = i;
}
// a固定下来,b逐个dfs过去
for(int i=0;i<16;i++) {
count = 0;
memset(sign, 0, sizeof(sign));
memset(b, 0, sizeof(b));
dfs(0, i+1);
cout << "num=" << i+1 << ' ' << "count=" << count << endl;
}
}
于是获取到:
num=1 count=1
num=2 count=0
num=3 count=3
num=4 count=0
num=5 count=15
num=6 count=0
num=7 count=133
num=8 count=0
num=9 count=2025
num=10 count=0
num=11 count=37851
num=12 count=0
num=13 count=1030367
num=14 count=0
num=15 count=36362925
所以可以提交代码了:
#include<iostream>
using namespace std;
long long int a[17],b[17];
int mod = int(1e9)+7;
int main() {
a[1]=1;
a[3]=3;
a[5]=15;
a[7]=133;
a[9]=2025;
a[11]=37851;
a[13]=1030367;
a[15]=36362925;
b[1]=1;
for(int i=2;i<=16;i++) {
b[i] = (b[i-1] * i) % mod;
}
long long int n;
cin >> n;
cout << a[n]*b[n]%mod;
}
CF285D.D. Permutation Sum的更多相关文章
- CF285D.Permutation Sum
想了很久觉得自己做法肯定T啊,就算是CF机子的3s时限,但我毕竟是 O ( C(15,7)*7!*log ) .... 果然在n=15的点T了...贱兮兮地特判了15过掉了,结果发现题解说就是打表.. ...
- codeforces 285 D. Permutation Sum 状压 dfs打表
题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...
- SPOJ:Elegant Permuted Sum(贪心)
Special Thanks: Jane Alam Jan*At moment in University of Texas at San Antonio - USA You will be give ...
- Codeforces Round #175 (Div. 2)
A. Slightly Decreasing Permutations 后\(k\)个倒序放前面,前\(n-k\)个顺序放后面. B. Find Marble 模拟. C. Building Perm ...
- Codeforces Round #175 (Div. 2) A~D 题解
A.Slightly Decreasing Permutations Permutation p is an ordered set of integers p1, p2, ..., pn, c ...
- combination sum、permutation、subset(组合和、全排列、子集)
combination sum I.permutation I.subsets I 是组合和.全排列.子集的第一种情况,给定数组中没有重复的元素. combination sum II.permut ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 266. Palindrome Permutation
题目: Given a string, determine if a permutation of the string could form a palindrome. For example,&q ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
随机推荐
- ❤️❤️爆肝3万字整理小白快速入门分布式版本管理软件:Git,图文并茂(建议收藏)--已码一万字❤️❤️
@ 目录 什么是Git SVN VS Git 什么是版本控制 安装Git 谁在操作? Git本地仓库 本地仓库构造 重点 Git基本操作 git add git commit git diff git ...
- python3之工程中必须依赖的__init__.py
1. __init__.py 1.1 什么是__init__.py 在Python3工程里,当python3检测到一个目录下存在__init__.py文件时,Python3就会把它当成一个模块(m ...
- serialVersionUID序列化版本号与ObjectOutputStream对象输入输出流
1. 观察ObjectOutputStream 我们观察ObjectOutputStream就可以发现该类没有无参构造,只有有参构造,所以他是一个包装流 2. 具体使用: public static ...
- Python setattr() 函数 ,Python super() 函数: Python 内置函数 Python 内置函数
描述 setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在. 语法 setattr 语法: setattr(object, name, value) 参数 object -- ...
- 使用vxe table组件时,edit-render配置$select',选中option后关闭cell的激活状态,显示选中option的value问题
在我的项目中使用vxe table组件时,edit-render配置{name: '$select', options: [{label:"脉搏",value:"maib ...
- 开启Nginx代理HTTPS功能
1.首先查看是否已经安装SSL openssl version -a 2.生成SSL证书 在nginx目录下创建ssl文件夹 cd /etc/pki mkdir nginx cd nginx 生成20 ...
- Fiddler抓包一键生成代码
首先我们的需求场景是 用Fiddler抓到某个接口调用后,用代码来模拟调用,一般我们写代码会有3个步骤: 1设置http请求相关的参数:header,method,url,cookie等 2设置pos ...
- AES解密尾部出现乱码问题
说明 在使用AES解密的时候我发现解密出来的字符串尾部一直都有乱码 解决方案 尾部字符串的ascii码就是删除位索引 具体代码: cryptor = AES.new('AES_KEY'.encode( ...
- 八大排序算法之基数排序(python实现)
[写在前面] 参考文章: https://blog.csdn.net/nrsc272420199/article/details/82691596[给出的示例图,简单易懂,但是对于没一轮循环没有讲解的 ...
- diff算法深入一下?
文章转自豆皮范儿-diff算法深入一下 一.前言 有同学问:能否详细说一下 diff 算法. 简单说:diff 算法是一种优化手段,将前后两个模块进行差异化比较,修补(更新)差异的过程叫做 patch ...