Codeforces-1327D Infinite Path

Codeforces-1327D Infinite Path

You are given a colored permutation \(p_1, p_2, \dots, p_n\). The i-th element of the permutation has color \(c_i\).

Let's define an infinite path as infinite sequence \(i, p[i], p[p[i]], p[p[p[i]]] \dots\) where all elements have same color (\(c[i] = c[p[i]] = c[p[p[i]]] = \dots\)).

We can also define a multiplication of permutations a and b as permutation \(c = a \times b\) where \(c[i] = b[a[i]]\). Moreover, we can define a power k of permutation p as \(p^k=\underbrace{p \times p \times \dots \times p}_{k \text{ times}}\).

Find the minimum \(k > 0\) such that \(p^k\) has at least one infinite path (i.e. there is a position i in \(p^k\) such that the sequence starting from i is an infinite path).

It can be proved that the answer always exists.

Input

The first line contains single integer T (\(1 \le T \le 10^4\)) — the number of test cases.

Next 3T lines contain test cases — one per three lines. The first line contains single integer n (\(1 \le n \le 2 \cdot 10^5\)) — the size of the permutation.

The second line contains n integers \(p_1, p_2, \dots, p_n (1 \le p_i \le n, p_i \neq p_j for i \neq j)\) — the permutation p.

The third line contains n integers \(c_1, c_2, \dots, c_n (1 \le c_i \le n)\) — the colors of elements of the permutation.

It is guaranteed that the total sum of n doesn't exceed \(2 \cdot 10^5\).

Output

Print T integers — one per test case. For each test case print minimum k > 0 such that \(p^k\) has at least one infinite path.

Example

input

3
4
1 3 4 2
1 2 2 3
5
2 3 4 5 1
1 2 3 4 5
8
7 4 5 6 1 8 3 2
5 3 6 4 7 5 8 4

output

1
5
2

Note

In the first test case, \(p^1 = p = [1, 3, 4, 2]\)and the sequence starting from \(1: 1, p[1] = 1, \dots\) is an infinite path.

In the second test case, $p^5 = [1, 2, 3, 4, 5] $and it obviously contains several infinite paths.

In the third test case, \(p^2 = [3, 6, 1, 8, 7, 2, 5, 4]\) and the sequence starting from \(4: 4, p^2[4]=8, p^2[8]=4, \dots\) is an infinite path since \(c_4 = c_8 = 4\).

The meaning of problems

Given an arrangement, each element has a color, the smallest Q k, such that the sequence \ (p ^ k = \ underbrace {p \ times p \ times \ dots \ times p} _ {k \ text {times}} \ ) an infinite sequence present in \ (i, p [i] , p [p [i]], p [p [p [i]]] \ dots \)

answer

First, irrespective of color, the plurality of rings arranged in a certain form, for example 2 sample form 1-> 2-> 3-> 4-> 5- a length> 1 to 5 rings ( \ (P [1] = 2,1 to 2, P [P [. 1]] =. 3 \ DOTS \) , and this \ (p ^ k \) operation, is in fact equivalent to step k, down on the ring (since \ (p ^ k \) is \ (P \ {underbrace [P [P ...]] k} _ \) ), while regarded as a theorem thing is, loop length \ (L \) , in steps of k , then passes the point is \ (starting point + n * gcd (l, k ) \) points, so for us a ring, we do not need to enumerate all the steps, only enumerated as \ (l \ ) factor step further enumeration <Start factor can complete enumeration of all the cases, the starting point for the enumeration, we direct violence across the ring again check to see whether the same color, the same color all the updated answer,

This time complexity is:

For each factor, the starting point enumeration k, violent Check: \ (\ K FRAC {L} \) , this is divided into \ (K * L / K = L \) , for each factor is the complexity level girth the total number does not factor too much, probably log level, each ring is \ (L * log L \) , for all of the rings, the complexity of the approaching \ (nlogn \) , you can easily fall over, ran only a 140ms +

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 50;
int p[N], c[N];
vector<int> a[N];
int vis[N];
int main() {
    int t; scanf("%d", &t);
    while (t--) {
        int n; scanf("%d", &n);
        for (int i = 1; i <= n; i++) vis[i] = 0, a[i].clear();
        for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
        for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) {
                vis[i] = 1;
                a[++cnt].push_back(i);
                int now = i;
                while (1) {
                    now = p[now];
                    vis[now] = 1;
                    if (now != i) a[cnt].push_back(now);
                    else break;
                }
            }
        }
        int ans = 1e9;
        for (int i = 1; i <= cnt; i++) {
            for (int j = 1; j <= a[i].size(); j++) {
                if (a[i].size() % j == 0) {
                    
                    for (int x = 0; x < j; x++) {
                        int cc = c[a[i][x]];
                        bool flag = true;
                        for (int k = x; k < a[i].size(); k += j) {
                            if (c[a[i][k]] != cc) {
                                flag = false;
                                break;
                            }
                        }
                        if (flag) ans = min(ans, j);
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

E title comes with a water problem

Codeforces-1327E Count The Blocks

The meaning of problems

Given a n, seeking \ (10 ^ n-1 \ ) within all numbers written length n number (less than fill prefix 0), the length is the number of digital block number i, respectively

answer

Consider n = 1, then the block length may be only one, the number is 10

In consideration of n = 2, then the total length of 100 * 2 = 200, digital block length is equal to the length of two digital block basis with a same first digit numbers, the number is equal to n = 1 1 the length of block 1 is the number of digits, the word length of the number is the total length of the block 2 is subtracted \ (2 * number of length 2 \)

Consider n = k, then the total length is the 10 ^ k * k, the number of words of length equal to the block n n = k-1 is the length of the number of word blocks n-1 (the same number plus one), the other empathy They are one and the same numbers plus its length plus one, so that we can direct recursion, and can maintain two prefixes

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
typedef long long ll;
const ll mod = 998244353;
ll ans[N];
ll suf1[N];
ll suf2[N];
int main() {
    int n;
    scanf("%d", &n);
    ans[n] = 10;
    ll base = 10;
    
    ll cnt = 1;
    suf1[n] = 10;
    suf2[n] = 10;
    for (int i = n - 1; i >= 1; i--) {
        base = base * 10 % mod;
        cnt++;
        ll xbase = base * cnt % mod;
        ans[i] = (xbase - suf1[i + 1] - suf2[i + 1] + mod + mod) % mod;
        suf1[i] = (suf1[i + 1] + ans[i] + mod) % mod;
        suf2[i] = (suf2[i + 1] + suf1[i] + mod) % mod;
    } 
    for (int i = 1; i <= n; i++) printf("%lld ", ans[i] % mod)
    return 0;
}

Guess you like

Origin www.cnblogs.com/artoriax/p/12564515.html