CF698F Coprime Permutation
题意:求有多少种符合要求的排列满足对于所有i,j,当gcd(i,j)=1时,gcd(pi,pj)=1。
排列上的一些位置给出。
标程:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+;
const int N=;
int n,p[N],cnt[N],Cnt[N],base[N],To1[N],To2[N],jc[N],x,ans;
vector<int> fac[N];
void fail(){puts("");exit();}//这个东西超级好用!
void shai()
{
for (int i=;i<=n;i++)
if (!p[i])
{
cnt[n/i]++;
for (int j=i;j<=n;j+=i)
{
p[j]=;base[j]*=i;
fac[j].push_back(i);
}
}
}
void check(int x,int y)
{
if (fac[x].size()!=fac[y].size()) fail();
for (int i=;i<fac[x].size();i++)
{
int fu=fac[x][i],fv=fac[y][i];
int u=(x==)?:n/fu,v=(y==)?:n/fv;
if (u!=v) fail();
if (To1[fu]&&To1[fu]!=fv) fail();
if (To2[fv]&&To2[fv]!=fu) fail();
if (!To2[fv]) To1[fu]=fv,To2[fv]=fu,cnt[u]--;
}
Cnt[base[x]]--;
}
int main()
{
scanf("%d",&n);
jc[]=;cnt[]=;//1和所有数互质!!!
for (int i=;i<=n;i++) jc[i]=(ll)jc[i-]*i%mod,base[i]=;
shai();fac[].push_back();//!!!
for (int i=;i<=n;i++) Cnt[base[i]]++;
for (int i=;i<=n;i++)
{
scanf("%d",&x);
if (x) check(i,x);
}
ans=;
for (int i=;i<=n;i++)
ans=(ll)ans*jc[cnt[i]]%mod*jc[Cnt[i]]%mod;
printf("%d\n",ans);
return ;
}
易错点:1.注意1和所有数互质,所以cnt[1]=1,表示1~n和1不互质的只有1个数。
2.fac[1].push_back(1),1有一个因数为1,小心判错。
题解:数学+性质
一开始我想分别求出与每个数互质的数的个数,较难。
发现可以从什么样的数相互交换等价入手:1.两个数的因数种类完全一样。2.若质数p1,p2,且[n/p1]=[n/p2]时,即1~n中所有p1的倍数和p2的倍数可以一一对应,那么对应互换。这两个交换相互独立。
如果没有固定元素这样就结束了。
判断合法性:
1.两个元素的因数去重后的个数要一样。
2.两个限制可以合并为对应因数的出现次数一样。
3.质因子之间产生轮换,可以会产生矛盾,要判掉。
最后减去已经确定的答案。
实现的时候有一些小技巧:
1.比较因数种类完全一样时,相当于比较两个数所有质因子的一次乘积。
2.可以用素数筛求出所有质数并筛出每个数的因数种类。
3.当p1,p2<=n^0.5时,[n/p1]与[n/p2]必然不等。
CF698F Coprime Permutation的更多相关文章
- Codeforces 698F - Coprime Permutation(找性质)
Codeforces 题面传送门 & 洛谷题面传送门 u1s1 感觉这个 D1F 比某道 jxd 作业里的 D1F 质量高多了啊,为啥这场的 D 进了 jxd 作业而这道题没进/yun 首先这 ...
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
随机推荐
- 如何发现 Redis 热点 Key ,解决方案有哪些?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 来源:http://t.cn/EAEu4to 一.热点问题产生原因 热点问题产生的原因大致有以下两种: 1.1 用户消费的数 ...
- python面试题之你如何管理不同版本的代码?
答案: 版本管理!被问到这个问题的时候,你应该要表现得很兴奋,甚至告诉他们你是如何使用Git(或是其他你最喜欢的工具)追踪自己和奶奶的书信往来.我偏向于使用Git作为版本控制系统(VCS),但还有其他 ...
- 【CF886E】Maximum Element
题目 考虑正难则反,答案即为\(n!-\text{返回值为n的排列数}\) 一个排列的返回值为\(n\),当且仅当在\(n\)出现之前没有一个数后面有连续\(k\)个小于它的数 设\(f_i\)表示\ ...
- 初识 flex 布局
开启弹性盒模式: display:flex / inline-flex: inline-flex 行内弹性盒 1.设置 flex 缩放的 限定值 min-width 最小值 min-wi ...
- unlocked - 非锁定的标准输入输出函数
SYNOPSIS 总览 #include <stdio.h> int getc_unlocked(FILE *stream); int getchar_unlocked(void); in ...
- 深度探索C++对象模型读书笔记-第六章执行期语意学
在函数中,编译器会帮助将析构函数(Destructor) 安插在相应的位置.对于函数中的局部对象,会将析构函数安插在对象的每一个离开点. 例如: 1: void Function(int a) { 2 ...
- AES加密php,java,.net三种语言同步实现加密、解密
话不多数上代码: java::: /* * To change this license header, choose License Headers in Project Properties. * ...
- 36. 解决线程问题方式一(同步代码块synchronized)
解决线程问题: 方式一:同步代码块(synchronized) 语法: synchronized ("锁对象") { //需要锁定的代码 } ...
- leetcood学习笔记-404-左叶子之和
题目描述: 方法一:递归 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: retur ...
- Delphi 字符串函数 StrUtils(大全)
引用单元: StrUtils; 首部 function AnsiResemblesText(const AText, AOther: string): Boolean; $[StrUtils.pas ...