poj 2369 Permutations (置换入门)
题意:给你一堆无序的数列p,求k,使得p^k=p
思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 1234
using namespace std; int gcd(int a,int b)
{
if(b==) return a;
else return gcd(b,a%b);
} int lcm(int a,int b)
{
return a*b/gcd(a,b);
} int main(int argc, char const *argv[])
{
int n;
int data[maxn];
bool visit[maxn];
int ans;
while(cin>>n)
{
for(int i=;i<=n;i++)
cin>>data[i];
ans=;
memset(visit,false,sizeof(visit));
for(int i=;i<=n;i++)
{
if(!visit[i])
{
int len=;
visit[i]=true;
int tmp = data[i];
while(tmp!=i)
{
visit[tmp]=true;
tmp=data[tmp];
len++;
}
ans=lcm(ans,len);
}
}
cout<<ans<<endl;
}
return ;
}
poj 2369 Permutations (置换入门)的更多相关文章
- poj 2369 Permutations 置换
题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...
- POJ 2369 Permutations (置换的秩P^k = I)
题意 给定一个置换形式如,问经过几次置换可以变为恒等置换 思路 就是求k使得Pk = I. 我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数. 把一个置换转换成轮换的方法也 ...
- poj 2369 Permutations - 数论
We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- POJ 2369 Permutations
傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- poj 3270 Cow Sorting (置换入门)
题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
随机推荐
- 配置FindBugs和常见FindBugs错误
配置FindBugs: 在这里可以对FindBugs规则等进行详细设置. 选择你的项目,右键 => Properties => FindBugs => 1 Run Automatic ...
- groovy学习(五) 命令行输入输出
isr = new InputStreamReader(System.in);br = new BufferedReader(isr);name = br.readLine();println(&qu ...
- jquery序列化form表单
在开发中有时需要在js中提交form表单数据,就需要将form表单进行序列化. jquery提供的serialize方法能够实现. $("#searchForm").seriali ...
- screen printing
https://www.youtube.com/watch?v=kWKOgHaze0s sample website provide http://midwestsign.com/index.asp ...
- KEEP!
[list][*]别问我前端有没有前途,我不知道,我只知道我现在喜欢前端,以后也应该喜欢.[*]别问我前端的工作好不好找,不管哪一职位,工作好不好找都是看你的水平.[*]别问我前端累不累,这世界就没有 ...
- Smarty模板的基础
对前后端进行分离 如果要用的话,要从网上把smarty文件下载下来,才能用 smarty的核心是一个类 建一个php文件,写一个类文件 <?php class smarty { public $ ...
- ci 框架发送邮箱
定义数据 $config = array( 'protocol' =>'smtp', 'smtp_host'=>'ssl://smtp.163.com', 'smtp_u ...
- webots自学笔记(二)节点与机器人建模
原创文章,出自"博客园, _阿龙clliu" :http://www.cnblogs.com/clliu/ 上一次介绍了界面和一个简单的自由落体,然而在实际运用中,机器人的结构都是 ...
- Ionic2开发笔记(2)创建子页面及其应用
1. 当你第一次产生ionic2应用程序,这是生成的项目结构 ├── ├── config.xml 这包含配置应用程序的名称,和包名,将被用于我们的应用程序安装到一个实际的设备. ├── h ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板
1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 854 Solved: 42 ...