zoj4006 & ZOJ 2018三月月赛C(扩展欧几里德,lucas定理)

151 - ZOJ Monthly, March 2018 - C
Travel along the Line

Time Limit: 1 Second       Memory Limit: 65536 KB

BaoBao is traveling along a line with infinite length.

At the beginning of his trip, he is standing at position 0. At the beginning of each second, if he is standing at position , with  probability he will move to position , with  probability he will move to position , and with  probability he will stay at position . Positions can be positive, 0, or negative.

DreamGrid, BaoBao's best friend, is waiting for him at position . BaoBao would like to meet DreamGrid at position  after exactly  seconds. Please help BaoBao calculate the probability he can get to position  after exactly  seconds.

It's easy to show that the answer can be represented as , where  and  are coprime integers, and  is not divisible by . Please print the value of modulo , where  is the multiplicative inverse of  modulo .

Input

There are multiple test cases. The first line of the input contains an integer  (about 10), indicating the number of test cases. For each test case:

The first and only line contains two integers  and  (). Their meanings are described above.

Output

For each test case output one integer, indicating the answer.

Sample Input
3
2 -2
0 0
0 1
Sample Output
562500004
1
0



题意:给一个点原来在数组的原点,每过一秒它1/2可能不动,1/4可能向左一步,1/4可能向右一步,求n秒后它在点m的概率模1e9+7的结果。(P/Q)%mod可用P*Q^-(1)%mod表示,(Q^(-1)表示Q的逆元)。
思路:手动找规律。。发现可以把分子分母分开计算,n秒它的分母必定是4^n,用扩展欧几里德求它逆元即可,对于分子,找到规律是杨辉三角。利用杨辉三角计算公式C(n,m)计算处于杨辉三角第n行第m个数。(n,m都是0开始)

#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long LL;
LL n,m,p=1000000007; 
LL extgcd(LL a,LL b,LL&x,LL&y) //扩展欧几里德返回gcd(a,b) (x为a模b的逆元,y为b模a的逆元)
{
	LL d=a;
	if(b!=0)
	{
		d=extgcd(b,a%b,y,x);
		y-=(a/b)*x;
	}
	else
	{
		x=1;
		y=0;
	}
	return d;
} 
LL quick_mod(LL a, LL b)  
{  
    LL ans = 1;  
    a %= p;  
    while(b)  
    {  
        if(b & 1)  
        {  
            ans = ans * a % p;  
            b--;  
        }  
        b >>= 1;  
        a = a * a % p;  
    }  
    return ans;  
}  
LL C(LL n, LL m)  
{  
    if(m > n) return 0;  
    LL ans = 1;  
    for(int i=1; i<=m; i++)  
    {  
        LL a = (n + i - m) % p;  
        LL b = i % p; 
        ans = ans * (a * quick_mod(b, p-2) % p) % p;  
    }  
    return ans;  
}  
LL Lucas(LL n, LL m)  //计算C(n,m)%p,且p一定为素数
{  
    if(m == 0) return 1;  
    return C(n % p, m % p) * Lucas(n / p, m / p) % p;  
}  
int main()
{
	int t;
	LL n,m;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&m);
		if(abs(m)>n)
		{
			printf("0\n");
			continue;
		}
		n*=2;
		LL a=quick_mod(2,n);
		LL x=0,y=0;
		extgcd(a,p,x,y); 
		m=n/2+m;
		while(x<0)x+=p;
		printf("%lld\n",(Lucas(n,m)*x)%p);
	}
}










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值