@codeforces - 1205E@ Expected Value Again


@description@

Given two numbers n, k, let s be a random character string set size k.

Definition of f (s) represents the length satisfies s = s i is prefixed number i of length suffix i of claim 1 ≤ i <| s |.

Seeking f (s) ^ 2 is desired.

Input
only one line, comprising two integers n, k (1≤n≤10 ^ 5, 1≤k≤10 ^ 9), meaning as above.

Output
Output desirable mod 10 ^ 9 + 7.

Examples
Input
2 3
Output
333333336

Input
1 5
Output
0

Input
100 1
Output
9801

Input
10 10
Output
412377396

@solution@

@part - 1@

In fact, before the suffix is equal to said subject, that is, the number of border.
Because the border length k of length equivalent to n - k periods, in order to facilitate the following discussion, we wish to calculate the number of cycles.

Let the random variables considered \ (G_i (s) \) , when the period length comprises i s is 1, and 0 otherwise.
Can be obtained \ (F (S) = \ sum_ = {I}. 1. 1} ^ {n--G_i (S) \) , i.e., considering the contribution of each period length.
Can then be obtained \ (E (f (s) ^ 2) = \ sum_ {i = 1} ^ {n-1} \ sum_ {j = 1} ^ {n-1} E (g_i (s) * g_j ( S)) \) , the linear nature of this calculation may be desirable.

Consider \ (E (g_i (s) * g_j (s)) \) is substantially equal to the length of the cycle containing both s length i is the probability of the period j.
There is a conclusion: \ (E (G_i (S) * G_J (S)) = (\ FRAC {. 1} {K}) ^ {n-- \ max (I + JN, GCD (I, J))} \ ) . I will prove in part - given 3.
Next, consider the answer to solving In the case of this conclusion premise.

@part - 2@

According to the above ideas, we want to ask \ (\ sum_ {i = 1 } ^ {n-1} \ sum_ {j = 1} ^ {n-1} (\ frac {1} {k}) ^ { n-- \ max (I + JN, GCD (I, J))} \) .

Note that this equation with only \ (s = i + j, d = gcd (i, j) \) related, but because \ (D | S \) , so the number of tuples (d, s) is not more than O (nlog n) (The harmonic series).
We tried for each pair (d, s) is calculated on the number of (i, j) satisfies \ (S = I + J, GCD (i, j) = D \) .

Since \ (gcd (i, S) = gcd (i, S - i) = gcd (i, j) = D \) (depending on the nature of gcd), so j is actually immaterial, the only constraint to take i value range.
We need only count the number of i satisfying 1 <= i <= n- 1 and 1 <= s - i <= n-1 and \ (GCD (S, i) = D \) .

Inequality direct solution, it must be a solution anyway intervals. Only you need to consider \ (gcd (s, i) = d \) case.
This theory, use the Mobius inversion, but in fact i just press descending count, enumerate every time a satisfying p i | p | s, then the answer i answer out of inclusion and exclusion of p . Because descending calculation, the processing will correct answer to p i corresponding to a certain process out.

The time complexity is i | p | triples the number of such s, rough analysis should be \ (O (n \ log ^ 2n) \) level.

@part - 3@

We formally consider how to prove \ (E (g_i (s) * g_j (s)) = (\ frac {1} {k}) ^ {n - \ max (i + jn, gcd (i, j))} \) .

A first built FIG n points, if there is a cycle of length x, the connection with all edges (y, y + x).
Down so connected, the same value of a block communicating necessarily the same. Assuming that the number of communication block is c, then there will be a total of k ^ c different strings.
And because the total number of strings of k ^ n, then the probability of \ (\ frac {k ^ c } {k ^ n} = (\ frac {1} {k}) ^ {nc} \)

First, when i + j <= n, according to the Lemma weak period (Kinzer of the WC search string can be found to courseware), there must gcd (i, j) of period, the number of blocks communicated c = gcd (i , j).
And because i + j <time = n gcd (i, j) > 0> = i + j - n, so our answer correct formula.

Next, consider i + j> n, the loss of generality, so i> j. And because i + j> n, so that i> n - j.
We first consider the original dpi cycle i n connected by a length of the edge, even all the (y, y + i). Even the number of such communication block out some of i, and representing the cycles of length i in the 0 ... i-1 for all positions (note that this is numbered from 0).
This communication might press shown in the block cycles of length i in a position to block communication are numbered 0 ... i-1.

Next, for each of a 0 <= z <n - j, we even (z, (z + j) mod i). Note that z <n - j <i.
Each addition of an edge in the case will not form a ring such that the number of blocks to reduce a communication. But if the formation of a ring, the conclusion is not established.
But even the side view of the above fact, the formation of a permutation of the first part, and this type of replacement in doing polya template used it to question the nature of:
(1) If the ultimate form multiple rings, the rings of all sizes must is i / gcd (i, j) .
(2) if added (z, (z + j) mod i) forms a ring, then all of the edges are then added to form a ring.
Thus, we can conclude: If a ring is formed, then the number of blocks communicated c necessarily gcd (i, j); and z is formed of a first ring = i - gcd (i, j ).

Thus, if z = i - gcd (i, j) <n - j, i.e., i + j - n <gcd ( i, j), then c = gcd (i, j) ; otherwise, c = i - (n - j) = i + j - n .
At this point, our conclusions will prove finished.

@solution@

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 100000;
const int MOD = int(1E9) + 7;
int gcd(int a, int b) {
    return (b == 0) ? a : gcd(b, a % b);
}
int pow_mod(int b, int p) {
    int ret = 1;
    while( p ) {
        if( p & 1 ) ret = 1LL*ret*b%MOD;
        b = 1LL*b*b%MOD;
        p >>= 1;
    }
    return ret;
}
int n, m, k, ans;
void update(int d, int s, int p) {
    ans = (ans + 1LL*p*pow_mod(k, MOD - 1 + max(s - n, d) - n)%MOD)%MOD;
}
vector<int>dv[2*MAXN + 5];
void init() {
    for(int i=1;i<=m;i++)
        for(int j=i;j<=m;j+=i)
            dv[j].push_back(i);
}
int f[2*MAXN + 5];
int main() {
    scanf("%d%d", &n, &k), m = 2*(n - 1);
    init();
    for(int s=1;s<=m;s++) {
        int l = max(1, s - (n - 1)), r = min(n - 1, s - 1);
        for(int j=dv[s].size()-1;j>=0;j--) {
            int d = dv[s][j], p = s / d;
            f[d] = r / d - (l - 1) / d;
            for(int k=1;k<dv[p].size();k++)
                f[d] = (f[d] + MOD - f[d*dv[p][k]])%MOD;
            update(d, s, f[d]);
        }
    }
/*
    for(int i=1;i<=n-1;i++)
        for(int j=1;j<=n-1;j++)
            update(gcd(i, j), i + j, 1);
*/
    printf("%d\n", ans);
}

@details@

E so the original div1 water.

Like because I used the vector so it runs very slowly. . .
But not sure, maybe I might be time complexity accidentally write more about what a strange thing. . . .

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11402012.html