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. 一篇文章搞定Selenium元素定位/封装/数据驱动

    小伙伴都知道,自动化最重的,又最"难"(因为实战中会碰到定位的各种坑)那就是定位元素.如果不熟练掌握定位,那只怕你比功能测式的小伙伴下班还会要晚!扎心了吧! Selenium常用定 ...

  2. Flawfinder在Python2和Python3环境下对代码进行扫描方法

    1. Flawfinder Flawfinder是一款开源的关于C/C++静态扫描分析工具,其根据内部字典数据库进行静态搜索,匹配简单的缺陷与漏洞. 官网:https://dwheeler.com/f ...

  3. 【Markdown】Shell命令高亮显示

    [问题]shell命令,黏贴到简书的代码块上,#后面的命令显示成被注释掉的效果 image.png [目的]高亮显示shell命令 [方案1]在代码块标示符后,加上此代码块所用的语言名(请注意要用小写 ...

  4. VUE -input输入框字母转大写

    示例: 输入自动转--->大写 <input type="text" placeholder="请输入证件号码" maxlength="1 ...

  5. MacOS上通过虚拟机搭建基础CentOS7系统环境

    MacOS上通过虚拟机搭建基础CentOS7系统环境 尽管从Mac的Terminal可以看出,macOS与UNIX.Linux或多或少都有血缘关系(shell.bash等),但是在mac进行Linux ...

  6. t-SNE 从入门到放弃

    t-SNE 算法 1 前言 t-SNE 即 t-distributed stochastic neighbor embedding 是一种用于降维的机器学习算法,在 2008 年由 Laurens v ...

  7. 如何迁移 Spring Boot 到函数计算

    作者 | 田小单 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上 ...

  8. 一个关于MySQL指定编码实现的小坑

    写在前面 环境:MySQL5.7+,MySQL数据库字符编码实现为utf8,表也为utf8 场景:微信授权获取用户信息(包括昵称)并保存到数据库,有的用户成功了,少数用户却失败了 那么为什么会失败呢? ...

  9. 洛谷3203 弹飞绵羊(LCT)

    据说这个题当年的正解是分块qwq 根据题目所说,对于题目中的弹力系数,就相当于一条边,那么对于"跳出去"这个限制,我们可以选择建造一个新点\(n+1\)表示结束,那么每次,求一个点 ...

  10. CAD_DWG图Web可视化一站式解决方案-唯杰地图-vjmap

    背景 DWG图是AutoCAD是私有格式,只能在CAD软件上编辑查看,如何发布至Web上做数据展示,GIS分析应用开发,一直是业内头疼的事情. 传统的办法采用的解析AutoCAD图形绘制,并封装成Ac ...