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的更多相关文章

  1. CF285D.Permutation Sum

    想了很久觉得自己做法肯定T啊,就算是CF机子的3s时限,但我毕竟是 O ( C(15,7)*7!*log ) .... 果然在n=15的点T了...贱兮兮地特判了15过掉了,结果发现题解说就是打表.. ...

  2. codeforces 285 D. Permutation Sum 状压 dfs打表

    题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...

  3. SPOJ:Elegant Permuted Sum(贪心)

    Special Thanks: Jane Alam Jan*At moment in University of Texas at San Antonio - USA You will be give ...

  4. Codeforces Round #175 (Div. 2)

    A. Slightly Decreasing Permutations 后\(k\)个倒序放前面,前\(n-k\)个顺序放后面. B. Find Marble 模拟. C. Building Perm ...

  5. Codeforces Round #175 (Div. 2) A~D 题解

    A.Slightly Decreasing Permutations Permutation p is an ordered set of integers p1,  p2,  ...,  pn, c ...

  6. combination sum、permutation、subset(组合和、全排列、子集)

    combination sum I.permutation I.subsets  I 是组合和.全排列.子集的第一种情况,给定数组中没有重复的元素. combination sum II.permut ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. 266. Palindrome Permutation

    题目: Given a string, determine if a permutation of the string could form a palindrome. For example,&q ...

  9. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

随机推荐

  1. P2150-[NOI2015]寿司晚宴【dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P2150 题目大意 将\(2\sim n\)选出一些分成两个集合,要求这两个集合中没有一对数不是互质的.求方案数对\ ...

  2. Java实现两数之和等于二十

    找出数组中两个数字之和为20的两个数 代码实现 public static void main(String[] args) { // TODO Auto-generated method stub ...

  3. 在你面前有一个n阶的楼梯,你一步只能上1阶或2阶。 请问,当N=11时,你可以采用多少种不同的方式爬完这个楼梯();当N=9时呢?

    在你面前有一个n阶的楼梯,你一步只能上1阶或2阶.请问,当N=11时,你可以采用多少种不同的方式爬完这个楼梯:当N=9时呢? 思路解析 ①台阶只有一级阶梯时,只有一种走法. ②当台阶有两级时,可以先走 ...

  4. Django实现用户登录注册

    本文将会介绍小白如何完成一个用户登录注册系统 新建一个Django项目,名字为login_register,并且使用命令manage.py startapp.User(名字自己随便起) 最终djang ...

  5. 【NOIP1998】 三连击 题解

    文章转载前需和原作者联系,否则追究法律责任 题目链接:https://www.luogu.com.cn/problem/P1008 首先我们来分析一下题目.要求是枚举三个数,比例为1:2:3,且各个数 ...

  6. Python中生成器的理解

    1.生成器的定义 在Python中一边循环一边计算的机制,称为生成器 2.为什么要有生成器 列表所有的数据都存在内存中,如果有海量的数据将非常耗内存 如:仅仅需要访问前面几个元素,那后面绝大多数元素占 ...

  7. Ajax样例

    $.ajax({ url : "newsservlet",//请求地址 dataType : "json",//数据格式 type : "post&q ...

  8. part1 软件测试基础知识面试题(含答案)

    1.你的测试职业发展是什么? 测试经验越多,测试能力越高.所以我的职业发展是需要时间积累的,一步步向着高级测试工程师奔去.而且我也有初步的职业规划,前3年积累测试经验,按如何做好测试工程师的要点去要求 ...

  9. JVM详解(六)——对象的实例化、内存布局与访问定位

    一.对象的实例化 1.创建对象的方式 2.创建对象的步骤 脑图:https://www.processon.com/view/link/61701a927d9c087040525226 3.对象属性赋 ...

  10. 网格布局GirdLayout在py中的引用,用于多行多列矩阵

    """ GridLayout为网格布局为了部件为多行距阵 """ from kivy.uix.gridlayout import GridL ...