0
点赞
收藏
分享

微信扫一扫

2022牛客五一集训派对day5:Eddy Walker

您好 2022-05-04 阅读 42
c++算法

链接:A-Eddy Walker_2022牛客五一集训派对day5 (nowcoder.com)
来源:牛客网

题目描述

Eddy likes to walk around. Especially, he likes to walk in a loop called "Infinite loop". But, actually, it's just a loop with finite length(Anyway, the name doesn't matter). Eddy can walk in a fixed length. He finds that it takes him N steps to walk through the loop a cycle. Then, he puts N marks on the "Infinite loop" labeled with 0,1,…,N−10, 1, \ldots, N-10,1,…,N−1, where i and i+1 are a step away each other, so as 0 and N-1. After that, Eddy stands on the mark labeled 0 and start walking around. For each step, Eddy will independently uniformly randomly choose to move forward or backward. If currently Eddy is on the mark labeled i, he will on the mark labeled i+1 if move forward or i-1 if move backward. If Eddy is on the mark labeled N-1 and moves forward, he will stand on the mark labeled 0. If Eddy is on the mark labeled 0 and moves backward, he will stand on the mark labeled N-1.

Although, Eddy likes to walk around. He will get bored after he reaches each mark at least once. After that, Eddy will pick up all the marks, go back to work and stop walking around.

You, somehow, notice the weird convention Eddy is doing. And, you record T scenarios that Eddy walks around. For i-th scenario, you record two numbers NiN_iNi​, MiM_iMi​, where NiN_iNi​ tells that in the i-th scenario, Eddy can walk through the loop a cycle in exactly NiN_iNi​ steps(Yes! Eddy can walk in different fixed length for different day.). While MiM_iMi​ tells that you found that in the i-th scenario, after Eddy stands on the mark labeled MiM_iMi​, he reached all the marks.

However, when you review your records, you are not sure whether the data is correct or even possible. Thus, you want to know the probability that those scenarios will happen. Precisely, you are going to compute the probability that first i scenarios will happen sequentially for each i.\textbf{Precisely, you are going to compute the probability that first i scenarios will happen sequentially for each i.}Precisely, you are going to compute the probability that first i scenarios will happen sequentially for each i.

输入描述:

The first line of input contains an integers T.
Following T lines each contains two space-separated integers NiN_iNi​ and MiM_iMi​.

1≤T≤10211 \leq T \leq 10211≤T≤1021
0≤Mi<Ni≤1090 \leq M_i < N_i \leq 10^90≤Mi​<Ni​≤109

输出描述:

Output T lines each contains an integer representing the probability that first i scenarios will happen sequentially.
you should output the number module 109+7(1000000007)10^9+7(1000000007)109+7(1000000007).
Suppose the probability is PQ\frac{P}{Q}QP​, the desired output will be P×Q−1mod  109+7P \times
Q^{-1} \mod 10^9+7P×Q−1mod109+7

示例1

输入

3
1 0
2 1
3 0

输出

1
1
0

题意:有0~n-1个数,闭环,从0开始向左或向右走,求经过所有点后停留在m上的概率,答案对10e+7取余

思路:因为是从0开始,走完所有数字就停下,所以除非n=1的时候,m=0的概率为1,其余都为0,而当m为其它非零数时,概率皆为1/(n-1),还有一个注意点,t组数据的概率是统合一起的,输出前i个概率的积(如果其中的概率为0,后面的都可以直接输出0)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
static ll mod=1e9+7;
ll take(ll a,ll b)
{
    ll c=1;
    while(b)
    {
        if(b&1)
            c=c*a%mod;
        a=a*a%mod;
        b=b>>1;
    }
    return c;
}
int main()
{
    ll t,a=1;
	cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        if(n==1)
            a=a;
        else if(m==0)
            a=0;
        else
        {
            a=a*take(n-1,mod-2)%mod;
        }
        cout<<a<<endl;
    }
    
 } 
举报

相关推荐

0 条评论