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,数组中的数字互不相同,返回该数 ...
随机推荐
- w3cmark前端精彩博文周报 10.20-10.27
w3cmark 官方Q群 145423956 | 官方微博 @w3cmark 自从最近微博屏蔽了我的站点域名,就很懒了.毕竟和不爽,一个纯技术站点还被认为不安全链接,还申诉无门,那些所谓的客服都是自动 ...
- delphi execCommand
WebBrowser1.Document as IHTMLDocument2 关键点 function execCommand(const cmdID: WideString; showUI: Wor ...
- 安卓Activity界面切换添加动画特效
在Android 2.0之后有了overridePendingTransition() ,其中里面两个参数,一个是前一个activity的退出两一个activity的进入, @Override pub ...
- win7中USB音箱没有声音解决的方法
Win7装好后,原来在XP中工作正常的USB小音箱却不工作了,重装了声卡驱动还是一样,后来通过下面尝试最终好用了. 1.右键右下角喇叭button. 2.点击"播放设备". 3.设 ...
- Android 浅谈相机研发
在android中应用相机功能,一般有两种:一种是直接调用系统相机,一种自己写的相机. 我将分别演示两种方式的使用: 第一种:是使用Intent跳转到系统相机,action为:andro ...
- 正确率、召回率和 F 值
原文:http://peghoty.blog.163.com/blog/static/49346409201302595935709/ 正确率.召回率和 F 值是在鱼龙混杂的环境中,选出目标的重要评价 ...
- 随机森林实现 MATLAB
matlab 中随机森林工具箱的下载地址: http://code.google.com/p/randomforest-matlab/downloads/detail?name=Windows-Pre ...
- php引用详解
<?php function foo($bar){ $bar='111111111'; } $str='2222222222222'; foo($str); echo $str; //out ...
- c语言趣味
改编java趣味100题,c语言版 1.C语言版金字塔 #include <stdio.h> int main() { int n,i,j,k; scanf("%d", ...
- AJAX XMLHttpRequest
<html> <head> <title>XMLHTTPRequest对象的说明DEMO</title> <script language=&qu ...