2019牛客暑期多校训练营(第二场)A——Eddy Walker(逆元+找规律)

链接:https://ac.nowcoder.com/acm/contest/882/A
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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,…,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 NiNi, MiMi, where NiNi tells that in the i-th scenario, Eddy can walk through the loop a cycle in exactly NiNi steps(Yes! Eddy can walk in different fixed length for different day.). While MiMi tells that you found that in the i-th scenario, after Eddy stands on the mark labeled MiMi, 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.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 NiNi and MiMi.

1≤T≤10211≤T≤1021
0≤Mi

输出描述:

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)109+7(1000000007).
Suppose the probability is PQPQ, the desired output will be P×Q−1mod109+7P×Q−1mod109+7

示例1

输入

复制

3
1 0
2 1
3 0

输出

复制

1
1
0

题意:给出一个有0-n-1的环,初始在0,可以随机向前向后走一格,n个位置都走完,求最后走到m点的概率,坑点(最后求一个前缀积)

题解:如果n==1显然概率是1,如果m==0,从样例可以看出是0,其他情况是1/(n-1),为什么呢,打表找规律,这里有打表代码和AC代码:

打表代码:

#include 
#include 
#include 
#include 
using namespace std;
int num[520];
bool vis[520];
int main(){
	srand((unsigned)time(NULL));
	int n;
	while(cin >> n){
		memset(num,0,sizeof(num));
		for (int i = 1; i<= 10000000;i++){//数据开的大,跑得慢,耐心等待,可以调小一点 
			memset(vis,false,sizeof(vis));
			int pos=0;
			int cnt=1;
			vis[0]=true;
			while(cnt

 AC上代码:

t=int(input())
ans,mod = 1,1000000007
for i in range(t):
    n,m=map(int,input().split())
    if n==1: ans*=1
    elif m==0: ans=0
    else: ans=ans*pow(n-1,mod-2,mod)%mod
    print(ans)

 

你可能感兴趣的:(数论)