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[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
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 = 1; i <= N; ++i)
    {
        scanf("%d", &x[i]);
        w[i] = x[i];
    }
    for(int i = 1; i <= N; ++i)
    {
        int cnt = 1;
        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 0;
}
时间: 2024-10-10 06:08:34

URAL 1024 Permutations(LCM)的相关文章

最小公倍数(LCM)

题目描述 求a.b的最小公倍数. 题目分析 求a.b的最小公倍数,即求最小正整数c,使满足c%a=0且c%b=0. 代码实现 最大公约数(GCD) a*b/gcd(a, b)

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

辗转相除法(又称欧几里得算法)是求最大公因数的算法 要求a,b的最大公约数(a>b),我们可以递归地求b,a%b的最大公约数,直到其中一个数变成0,这时另一个数就是a,b的最大公约数. C++实现: int gcd(int a,int b){ retuen b?gcd(b,a%b):a; } 或: while(b!=0)  {  temp=a%b;   a=b;   b=temp; } 证明:(引自百度百科) 设两数为a.b(b<a),用gcd(a,b)表示a,b的最大公约数,r=a (mod

UVA11925-Generating Permutations(贪心)

Problem UVA11925-Generating Permutations Accept: 214  Submit: 1429Time Limit: 1000 mSec Problem Description A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these integers. Your task is to generate a given permu

数论(lcm)

CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to find such pair of indices i,j (1≤i<j≤n) that lcm(ai,aj) is minimum possible. lcm(x,y) is the least common multiple of x and y (minimum positive numbe

LeetCode Permutations (技巧)

题意: 给出n个元素,请产生出所有的全排列. 思路: 注意到可能会有相同的排列出现,比如 {2,2}.还有可能是乱序列(大部分情况下都是无所谓的). 递归法: 1 class Solution { 2 public: 3 void recursion(vector<int> num, int i, vector<vector<int> > &res) 4 { 5 if (i+1==num.size()) res.push_back(num); 6 else 7

URAL 1137Bus Routes (dfs)

Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1137 Description Several bus routes were in the city of Fishburg. None of the routes shared the same section of road, though commo

Ural 1119 Metro(DP)

题目地址:Ural 1119 因为还有一个可不可以穿的问题,所以需要再加一维.0代表可穿不可穿,可穿设置成0,不可穿就设置成无穷大.1代表当前这格的最短距离. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h

URAL 1290. Sabotage (sortings)

1290. Sabotage Time limit: 1.0 second Memory limit: 64 MB It is the seventh year of the terrible harmful Galaxy War... Leo Hao is one of the first defenders of his planet. He is lucky! He has gone through many troubles. For example, he stayed alive a

LeetCode OJ:Permutations(排列)

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 排列组合的问题,不用考虑是否发生重复的情况,代码如下: 1 class Solution { 2 public: 3 vector<vector<i