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)!+ ...
随机推荐
- 常用命令--sed
sed -n '/2019-07-24/,/2019-07-25/p' my.log sed -n '/2019-07-25 16:51:23,699/,/2019-07-25 16:51:38,79 ...
- java.io.FileNotFoundException: [WEB-INF/spring-servlet.xml] cannot be opened because it does not exist
今天启动web 项目出现错误提示: java.io.FileNotFoundException: [WEB-INF/spring-servlet.xml] cannot be opened becau ...
- leetcode.字符串.696计数二进制子串-java
1. 具体题目 给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的.重复出现的子串要计算它们出现的次数. 示例 1 : 输入: ...
- Excel简单应用
数据透视表的简单应用 LEN.LENB MID LEFT,RIGHT CONCATENATE 和 & TRIM(去掉前后空格) 和 SUBSTITUDE(去空格,包括字符串之间的空格) FIN ...
- 操作bin目录下的文件
string dir = AppDomain.CurrentDomain.BaseDirectory + "Video"; if (!System.IO.Directory.Exi ...
- CSS3-2d3d
1.过渡(transition)操作谁,给谁加过渡 transition:要过渡的属性 花费时间 运动曲线 何时开始: 多组属性变化,还是用 逗号 隔开 transition ...
- Spring框架4大原则和主要功能
Spring框架4大原则: 使用POJO进行轻量级和最小侵入式开发 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJ ...
- 链表list
Don't lost link! list与vector不同之处在于元素的物理地址可以任意. 为保证对列表元素访问的可行性,逻辑上互为前驱和后继的元素之间,应维护某种索引关系.这种索引关系,可抽象地 ...
- 处理Codeigniter CLI模式无法读取通过Apache写入的文件缓存
运行环境: Ubuntu 16.04 + PHP 5.6.40 + Apache/2.4.18 Codeigniter: 3.1.10 Codeigniter 3.1.10,缓存驱动方式是文件方式 遇 ...
- springBoot项目mybatis中加入缓存
1:maven: <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core --> <dependenc ...