题意:已知,可得出 P(1) = 4, P(2) = 1, P(3) = 5,由此可得出 P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3,因此。经过k次如上变换,最终可得,输入保证一定有解,求k。

分析:

1、能用数组表示映射就别用map,很耗时

2、序列中的每个数字经过这种变换都一定会变会自身,例如,一开始P(1) = 4,接下来P(P(1)) = P(4) = 2,再接下来P(P(P(1))) = P(P(4)) = P(2) = 1,只需三次,就可以变回自身(P(P(P(1))) =1),对序列中每个数字求次数,最终求这些次数的最小公倍数即可

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a < b ? b : a
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int x[MAXN];
int w[MAXN];
set<int> y;
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}
int N;
int main()
{
scanf("%d", &N);
for(int i = ; i <= N; ++i)
{
scanf("%d", &x[i]);
w[i] = x[i];
}
for(int i = ; i <= N; ++i)
{
int cnt = ;
while(x[i] != i){
x[i] = w[x[i]];
++cnt;
}
y.insert(cnt);
}
set<int>::iterator it = y.begin();
int ans = *it;
++it;
for(; it != y.end(); ++it){
ans = ans / gcd(ans, *it) * (*it);//求最小公倍数,调用函数耗时
}
printf("%d\n", ans);
return ;
}

URAL 1024 Permutations(LCM)的更多相关文章

  1. (Problem 62)Cubic permutations(待续)

    The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 ...

  2. 利用神经网络进行网络流量识别——特征提取的方法是(1)直接原始报文提取前24字节,24个报文组成596像素图像CNN识别;或者直接去掉header后payload的前1024字节(2)传输报文的大小分布特征;也有加入时序结合LSTM后的CNN综合模型

    国外的文献汇总: <Network Traffic Classification via Neural Networks>使用的是全连接网络,传统机器学习特征工程的技术.top10特征如下 ...

  3. LeetCode Permutations (全排列)

    题意: 给出n个元素,请产生出所有的全排列. 思路: 注意到可能会有相同的排列出现,比如 {2,2}.还有可能是乱序列(大部分情况下都是无所谓的). 递归(1):产生的过多的多余vector. cla ...

  4. UVA11925-Generating Permutations(贪心)

    Problem UVA11925-Generating Permutations Accept: 214  Submit: 1429Time Limit: 1000 mSec Problem Desc ...

  5. 最大公约数(gcd)和 最小公倍数(lcm)——辗转相除法

    辗转相除法(又称欧几里得算法)是求最大公因数的算法 要求a,b的最大公约数(a>b),我们可以递归地求b,a%b的最大公约数,直到其中一个数变成0,这时另一个数就是a,b的最大公约数. C++实 ...

  6. 数论(lcm)

    CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to ...

  7. URAL 1297 Palindrome(Manacher)

    The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...

  8. URAL 1119. Metro(BFS)

    点我看题目 题意  : 这个人在左下角,地铁在右上角,由很多格子组成的地图,每一条边都是一条路,每一条边都是100米.还有的可以走对角线,问你从起点到终点最短是多少. 思路 : 其实我想说一下,,,, ...

  9. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

随机推荐

  1. HDU 5538 L - House Building 水题

    L - House Building Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  2. 在.NET中实现彩色光标/动画光标和自定义光标[转]

    下面是完整的例子,可以通过命令行编译即可看到效果. Test.cs using System; using System.Drawing; using System.Windows.Forms; us ...

  3. pjsip视频通信开发(上层应用)之拨号界面整体界面功能实现

    在前面的几章里面写了显示.键盘.拨号.删除功能,这里我将他们进行组合,形成一个拨号键盘全部功能.首先是布局 <LinearLayout xmlns:android="http://sc ...

  4. visualsvn server 报错 can't read file "current":End of file out

    需要删除文件:Repositories\ProjectName\db\revprops\X 需要修改文件:repository/db/txn_current.repository/db/current ...

  5. 首发Zend Studio 10.6正式版注册破解(2014-02-06更新)

    1.写在前头以下文章没有图片说明,如果你没有耐性看或想看图文并茂的图片文章请绕道,谢谢配合.转发请标明转自http://www.geekso.com/ZendStudio100/以下方法仅供技术交流学 ...

  6. android147 360 程序锁

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. LVS 详解

    http://zh.linuxvirtualserver.org/node/25 http://chrinux.blog.51cto.com/6466723/1198748 http://www.cn ...

  8. Linux 内核动态函数调用可视化工具

    要求: python .7以上 https://www.python.org/ftp/python/ EG:wget --no-check-certificate https://www.python ...

  9. 【完结】利用 Composer 完善自己的 PHP 框架(三)——Redis 缓存

    本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 回顾 上两篇文章中我们完成了 View 视图加载类和 ...

  10. Mac Yosemite OS10.10 Apache 虚拟主机设置

    昨天睡觉前开始挂机下载OS10.10 Yosemite,早上6点半就很兴奋地起来安装新系统了.安装完成后打开界面,感觉真的大不一样了,很优很美,很喜欢. 在下载各种更新,体验各种新功能的同时,我也不忘 ...