题解 | Eddy Walk-2019牛客暑期多校训练营第二场A题

题目描述:
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−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 Ni,Mi,where Ni tells that in the i-th scenario, Eddy can walk through the loop a cycle in exactly Ni steps(Yes! Eddy can walk in different fixed length for different day.). While Mi tells that you found that in the i-th scenario, after Eddy stands on the mark labeled Mi , 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.

输入描述:
The first line of input contains an integers T.
Following T lines each contains two space-separated integers Ni and Mi

1≤T≤1021
0≤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).
Suppose the probability is P Q \frac{P}{Q} QP,the desired output will be P×Q-1 mod 109+7

示例1

输入
3
1 0
2 1
3 0

输出
1
1
0

题解:
·Math, Ad hoc, brute force
Try some combinations of small N, M. Found that probability = 1/(N - 1) for all m except 0

• Math, Ad hoc, brute force
Try some combinations of small N, M. Found that probability = 1/(N - 1) for all m except 0
Corner case: (N=1, M=0) = 1

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int mul(LL a, int b) {
  a *= b;
  return a >= mod ? a % mod : a;
}
int mpow(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) {
      ret = mul(ret, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return ret;
}
int inv(int a) {
  return mpow(a, mod - 2);
}
int prob(int ni, int mi) {
  if (ni == 1) {
    return 1;
  }
  if (mi == 0) {
    return 0;
  }
  return inv(ni - 1);
}
int main() {
  int ans = 1, t;
  cin >> t;
  while (t--) {
    int ni, mi;
    cin >> ni >> mi;
    ans = mul(ans, prob(ni, mi));
    printf("%d\n", ans);
  }
}

更多问题,更详细题解可关注牛客竞赛区,一个刷题、比赛、分享的社区。
传送门:https://ac.nowcoder.com/acm/contest/discuss

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值