2019 ICPC Shenyang network game J. Ghh Matin

Problem

Similar to the strange ability of Martin (the hero of Martin Martin), Ghh will random occurrence in one of \(N\) cities every morning and the money will change to \(X\) RMB (No matter how much money had yesterday).

Ghh finds that every \(N\) cities have only one way to enter and go out (for example, city A's out road is connected with city B's enter road, or city A's out road may also be connected with city A's enter road). After waking up one day, Ghh suddenly wants to do an experiment.

In the following days, Ghh will judge whether he has experimented in the city every time he wakes up. If not, he will go out to see if he can start from the city and finally return to itself. If he can't or has already done it, he will stay until the evening and go to the next random city by sleeping. This experiment lasts until every \(N\) cities have been done.

Because the distances between cities are very far, Ghh decides to take bus. The cost of bus between any two cities is \(1\) RMB (it also costs \(1\) RMB for the city that the enter road is connected with the out road of itself) and the speed is very fast. What is the probability that Ghh can return to the city itself from every city when Ghh has finished the experiment?

Note

When \(n=2\), there are two cases in city A and B:

  1. A connected with A, B connected with B
  2. A connected with B

When \(X\) is 1, only in the first case every city can return to itself, so the probability is 1/2. When \(X\) is 2, any case is ok.

Input

The first line, input a integer \(T\) represents the number of test cases (\(T \le 10^4\)). Next \(2\) ~ \(T+1\) line, input two integers \(N\), \(X\) represents the total number of cities and initial money (\(2≤N≤10^6\),\(N≤2X≤2×10^9\)). \(\sum{N} \le 10^7\).

Output

Each test case output one line, the probabilities may be \(a/b\), please output \(a*inv(b)\mod (10^9+7)\). \(inv(b)\) is the inverse of \(b\).

Sample input Copy

3
2 1
2 2 
145 134

Sample output copy

500000004
1
772215686

Comprehension

Meaning of the questions is probably saying: There are n cities, each city only in one out of the way (penetration, the same degree), from one city to another city for a dollar. Now I do not know what that figure is now needed from the start of each city (there are X dollars) to be back in the city, get this diagram what is the probability?

That is: Figure goal is definitely a lot of Euler composition diagram, ask each length Euler is not greater than the probability of X is the number?

Solve

According to the understanding of the meaning of the questions, we find bad enumerate such a graph, it is better to enumerate all of the conditions are not satisfied map, that is to look for the presence of Euler length greater than X in FIG.

Topic limit \ (N \ Le 2X \) , which means that there are X point of Euler tour, there is no X + 1's

Obviously, greater than X selected points connected to form a ring, the remaining free combination point.

We wish to point X is set longer than m, \ (X <m <N \)

(P (m) = \) \ present in an amount greater than the length X Euler FIG / total number of FIG.

Present in an amount greater than the length of the Eulerian X = N the number of which FIG selected from the m \ (\ Times \) m-. 1 (ring) the full array (because the starting point is fixed \ (\ Times \) nm (left point) of the full array

\(P(m)=C(m,N)*(m-1)!*(N-m)!/N!=1/m\)

So \ (ans = 1- \ sum \ limits_ {m> X} ^ N {P (m)} \)

Since \ (P (m) \) up to N, is 1e6, so open about prefixes and maintenance, save time

Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6+4;

ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans%mod;
}

ll inv(ll a){
    return qpow(a,mod-2);
}

ll sum[maxn];


int main()
{
    int t;
    ll n,x;
    for(int i = 1; i<maxn;i++){
        sum[i]=(sum[i-1]+inv(i))%mod;
    }
    scanf("%d",&t);
    while(t--) {
        scanf("%lld%lld", &n, &x);
        ll ans=1;
        if(n>x)
            ans = (ans+ mod -(sum[n]-sum[x]))%mod;
        cout<<ans<<endl;
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/tongseli/p/11730445.html
Recommended