[codeforces-542-C]YY?

來源:互聯網
上載者:User

標籤:

連結:http://codeforces.com/problemset/problem/542/C

題意:對一個函數f(x),定義域[1,n], 令f(k,x) = f(f(f(f...f(x))))(共迭代k次)。求最小的k,使得f(k, x) 滿足 g(g(x)) = g(x)的性質,也就是f(k,f(k,x)) = f(k,x)(x屬於[1,n])。

思路:方法雖然簡單,但比較巧妙,值得一做。由於f(k,x)只與f的迭代次數有關,而f數組一開始是給定的,所以不妨對所有的x屬於[1,n]單獨進行處理。考慮f(1,x),f(2,x),f(3,x)...,f(k,x)組成的序列,因為f(k,f(k,x)) = f(2k, x),所以轉化為求序列中第k位置的數Ak,使得Ak = A2k。由於序列的後一項是前一項進行一次f()操作得到的,所以必存在迴圈節,且迴圈節長度小於等於n,不妨令序列從P0位置開始迴圈,迴圈節長度為K0,則這種情況下的k的取值集合就是{sK0, (s+1)K0, (s+2)K0, ...},sK0為大於等於P0的最小數。然後對x取遍[1,n],求k的取值集合的交集元素的最小值便是答案,但顯然不能這樣求。由於對自變數的每個值,k的取值總是那種情況下的迴圈節K0的倍數,不妨先對所有的K0求一下最小公倍數,然後再調整結果使得它滿足大於等於所有的P0,調整的時候只需每次加上所有K0的最小公倍數,直到滿足條件。

  1 #pragma comment(linker, "/STACK:10240000,10240000")  2   3 #include <iostream>  4 #include <cstdio>  5 #include <algorithm>  6 #include <cstdlib>  7 #include <cstring>  8 #include <map>  9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21  22 using namespace std; 23  24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define mem_1(a) memset(a, -1, sizeof(a)) 26 #define lson l, m, rt << 1 27 #define rson m + 1, r, rt << 1 | 1 28 #define define_m int m = (l + r) >> 1 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 32 #define rep_down1(a, b) for (int a = b; a > 0; a--) 33 #define all(a) (a).begin(), (a).end() 34 #define lowbit(x) ((x) & (-(x))) 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 38 #define pchr(a) putchar(a) 39 #define pstr(a) printf("%s", a) 40 #define sstr(a) scanf("%s", a) 41 #define sint(a) scanf("%d", &a) 42 #define sint2(a, b) scanf("%d%d", &a, &b) 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 44 #define pint(a) printf("%d\n", a) 45 #define test_print1(a) cout << "var1 = " << a << endl 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 48 #define mp(a, b) make_pair(a, b) 49 #define pb(a) push_back(a) 50  51 typedef long long LL; 52 typedef pair<int, int> pii; 53 typedef vector<int> vi; 54  55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 57 const int maxn = 3e4 + 7; 58 const int md = 10007; 59 const int inf = 1e9 + 7; 60 const LL inf_L = 1e18 + 7; 61 const double pi = acos(-1.0); 62 const double eps = 1e-6; 63  64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 67 template<class T>T condition(bool f, T a, T b){return f?a:b;} 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 69 int make_id(int x, int y, int n) { return x * n + y; } 70  71 int f[207]; 72 int minv; 73  74 int find(int x) { 75     int used[207], c = 0; 76     mem0(used); 77     while (!used[f[x]]) { 78         c ++; 79         used[x = f[x]] = c; 80     } 81     max_update(minv, used[f[x]]); 82     return c - used[f[x]] + 1; 83 } 84  85 int main() { 86     //freopen("in.txt", "r", stdin); 87     int n; 88     cin >> n; 89     rep_up1(i, n) { 90         sint(f[i]); 91     } 92     LL ans = 1; 93     rep_up1(i, n) { 94         LL tmp = find(i); 95         ans = ans / gcd(ans, tmp) * tmp; 96     } 97     LL tmp = ans; 98     while (ans < minv) ans += tmp; 99     cout << ans << endl;100     return 0;101 }
View Code

 

[codeforces-542-C]YY?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.