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)!+ ...
随机推荐
- scala 实现算法
快速排序 def sort(xs: Array[Int]): Array[Int] = if (xs.length <= 1) xs else { val pivot = xs(xs.lengt ...
- sublimeText3的安装及插件的配置使用
这里主要记录一些关于sublime text的配置,并且参照了别人的博客归纳的. 一.下载sublime text http://www.sublimetext.com/3二.安装Package ...
- 【转】elasticsearch中字段类型默认显示{ "foo": { "type": "text", "fields": { "keyword": {"type": "keyword", "ignore_above": 256} }
官方原文链接:https://www.elastic.co/cn/blog/strings-are-dead-long-live-strings 转载原文连接:https://segmentfault ...
- springboot配置swagger-rest文档
前言 swagger是一个很好的restful形式的api文档,可以通过比较小的侵入来提供很好的restful的文档.因为swagger是依赖服务生成的,所以其实是依赖服务的,这也算是它的一个小缺点吧 ...
- ifame问题
问题描述:在div中添加了点击事件,div中嵌套着一个iframe,父类的div事件全部都会被失效, 解决办法:给iFrame添加css pointer-events : none; 去掉了ifram ...
- Replication Controller、Replica Set
假如我们现在有一个Pod正在提供线上的服务,我们来想想一下我们可能会遇到的一些场景: 某次运营活动非常成功,网站访问量突然暴增 运行当前Pod的节点发生故障了,Pod不能正常提供服务了 第一种情况,可 ...
- 泛型(Generic)接口
泛型接口例子:一个学生有一个独一无二的ID,但是每个学生的姓名不一定是唯一的. class Program { static void Main(string[] args) { Student< ...
- ps使logo背景色透明
方法一:魔法工具(对复杂的logo误差较大) 魔法工具--左键点击选区--delete--保存 方法二:拾色器 1.有的站上的素材图片不能直接用,需要先变成rgb图像,可这样操作:图像\模式,选择rg ...
- NX二次开发-UFUN将工程图中的点坐标映射到建模绝对坐标UF_VIEW_map_drawing_to_model
#include <uf.h> #include <uf_ui.h> #include <uf_draw.h> #include <uf_view.h> ...
- NX二次开发-打开文件夹,并同时选中指定文件
NX9+VS2012 #include <uf.h> #include <uf_ui.h> #include <uf_part.h> #include <at ...