URAL 1024 Permutations(LCM)
题意:已知
,可得出 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)的更多相关文章
- (Problem 62)Cubic permutations(待续)
The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 ...
- 利用神经网络进行网络流量识别——特征提取的方法是(1)直接原始报文提取前24字节,24个报文组成596像素图像CNN识别;或者直接去掉header后payload的前1024字节(2)传输报文的大小分布特征;也有加入时序结合LSTM后的CNN综合模型
国外的文献汇总: <Network Traffic Classification via Neural Networks>使用的是全连接网络,传统机器学习特征工程的技术.top10特征如下 ...
- LeetCode Permutations (全排列)
题意: 给出n个元素,请产生出所有的全排列. 思路: 注意到可能会有相同的排列出现,比如 {2,2}.还有可能是乱序列(大部分情况下都是无所谓的). 递归(1):产生的过多的多余vector. cla ...
- UVA11925-Generating Permutations(贪心)
Problem UVA11925-Generating Permutations Accept: 214 Submit: 1429Time Limit: 1000 mSec Problem Desc ...
- 最大公约数(gcd)和 最小公倍数(lcm)——辗转相除法
辗转相除法(又称欧几里得算法)是求最大公因数的算法 要求a,b的最大公约数(a>b),我们可以递归地求b,a%b的最大公约数,直到其中一个数变成0,这时另一个数就是a,b的最大公约数. C++实 ...
- 数论(lcm)
CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to ...
- URAL 1297 Palindrome(Manacher)
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...
- URAL 1119. Metro(BFS)
点我看题目 题意 : 这个人在左下角,地铁在右上角,由很多格子组成的地图,每一条边都是一条路,每一条边都是100米.还有的可以走对角线,问你从起点到终点最短是多少. 思路 : 其实我想说一下,,,, ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
随机推荐
- [AngularJS] Using ngModel in Custom Directives
You can use ngModel in your own directives, but there are a few things you'll need to do to get it w ...
- ios开发——实用技术篇&XML协议详解
XML的数据协议组成 名词 说明 md5 message-digest algorithm 5 http hypertext transfer protocol xml extensible mark ...
- oracle_partition sample
(1.) 表空间及分区表的概念 表空间: 是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表,所以称作表空间. 分区表: 当表中的数据量不断增大,查询数据的速度就会变慢 ...
- 嵌入式Linux开发系列之一: 走进嵌入式Linux的世界
转载:http://www.ibm.com/developerworks/cn/linux/l-embed/part1/index.html 随着信息化技术的发展和数字化产品的普及,以计算机技术. ...
- JavaScript与FileSystemObject
什么是FileSystemObject(简称FSO) FSO 即文件系统对象,是一种列表 Windows 磁盘目录和文件,对目录和文件进行删除.新建.复制.剪切.移动等操作的技术.使用 FSO 网站 ...
- vs2010 MFC执行流程
从大学时候学过到现在可能又要用到,搞这个东西真是痛苦.不过首先把繁琐的c++函数执行流程搞明白吧,好好多设置几个断点观测一下啦. Step1.在vs2010新建MFC工程,那么对应的代码页名字为:CS ...
- [改善Java代码]性能考虑,数组是首选
建议60:性能考虑,数组是首选 一.分析 数组在实际的系统开发中使用的越来越少,我们通常只有在阅读一些开源项目时才会看到它们的身影,在Java中它确实没有List.Set.Map这些集合使用起来方便 ...
- [未完成]WebService学习第一天学习笔记
[未完成]WebService学习第一天学习笔记[未完成]WebService学习第一天学习笔记
- Linux下is not in the sudoers file解决方法
最近在学习linux,在某个用户(xxx)下使用sudo的时候,提示以下错误:xxx is not in the sudoers file. This incident will be reporte ...
- JS轮播图
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...