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

本文探讨了一种特殊的行走问题——无限循环行走,在一个有限长度的循环路径中,通过随机前进或后退的方式达到每个标记点至少一次的概率计算。特别地,文章提供了具体的实现代码和分析方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: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<Ni≤1090≤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)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 <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
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<n){
				int x=rand()%2;
				if(!x) x=-1;
				pos+=x;
				pos=(pos+n)%n;
				if(!vis[pos]){
					vis[pos]=true;
					cnt++;
				}
				if(cnt==n){
					num[pos]++;
				}
			}
		}
		for (int i = 0; i< n;i++){
			cout << i << ": " << num[i] << endl; 
		}
	}
	return 0;
} 

 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)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值