cf935d(概率dp+逆元)

35 篇文章 0 订阅

这个dp倒是不难。。也算不上dp吧。。就是概率直接推而已。。

主要处理0的问题,分成大于和等于,把大于先算出来,等于留和给下一位乘上来。。

然后为什么要反过来算呢。 。就是比较方便算吧。。其实正着也没什么问题。。。

然后这个问的价值主要还是在他问的方式,就是把这个答案保留分数还取膜了,当然小数是不可能取膜的。。所以要把除法转变为乘法。。即把p/q (mod inf)变成p*q^(-1)mod inf。。当然在取膜意义上的-1次方可不是取倒数,而应该是求逆元。。即找到inv(q)使inv(q)*q≡1(mod inf)。。这样能将除法代替成乘法,方便了不少。。

这种问法以前在uoj上看过无数次然后被劝退了,学这个其实还挺重要的。。其实感觉这样的形式蛮好。。因为在未来的概率dp中,由于可能要转移上万次,精度就无法保证了,这样的形式可以优雅的避免精度问题。。逆元是个好东西。。。在窝学概率dp之前先来一波,这波还是很及时的(感谢

然后怎么求逆元还是以后去学吧。。今天先学一种,当inf为质数时,inv(x)=x^(inf-2),由费马小定理导出。。

另外。。。pow好像是cmath里有的函数。。。以后写快速幂就不要用这函数名了。。。这个pow也没见别人直接用,估计是有什么局限性把。。




/**
 *        ┏┓    ┏┓ 
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃   
 *        ┃   ━    ┃ 
 *        ┃ >   < ┃ 
 *        ┃       ┃ 
 *        ┃... ⌒ ...  ┃ 
 *        ┃       ┃ 
 *        ┗━┓   ┏━┛ 
 *          ┃   ┃ Code is far away from bug with the animal protecting           
 *          ┃   ┃   神兽保佑,代码无bug 
 *          ┃   ┃            
 *          ┃   ┃         
 *          ┃   ┃ 
 *          ┃   ┃            
 *          ┃   ┗━━━┓ 
 *          ┃       ┣┓ 
 *          ┃       ┏┛ 
 *          ┗┓┓┏━┳┓┏┛ 
 *           ┃┫┫ ┃┫┫ 
 *           ┗┻┛ ┗┻┛ 
 */  
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 300005
#define nm 100000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1000000007;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


int n,m,a[NM],b[NM];
ll d[NM],inv2,invm;
ll qpow(ll x,ll t){return t==0?1:qpow(sqr(x)%inf,t>>1)*(t&1?x:1)%inf;}
ll inv(int x){return qpow(x,inf-2);}

int main(){
	//freopen("data.in","r",stdin);
	n=read();m=read();
	inc(i,1,n)a[i]=read();
	inc(i,1,n)b[i]=read();
	invm=inv(m);inv2=inv(2);
	dec(i,n,1)
		if(a[i]&&b[i])
		if(a[i]==b[i])d[i]=d[i+1];else d[i]=a[i]>b[i];
		else if(b[i])d[i]=d[i+1]*invm%inf+(m-b[i])*invm%inf;
		else if(a[i])d[i]=d[i+1]*invm%inf+(a[i]-1)*invm%inf;
		else d[i]=d[i+1]*invm%inf+(m-1)*inv2%inf*invm%inf;
	return 0*printf("%I64d\n",d[1]%inf);
}






D. Fafa and Ancient Alphabet
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ancient Egyptians are known to have used a large set of symbols to write on the walls of the temples. Fafa and Fifa went to one of the temples and found two non-empty words S1 and S2 of equal lengths on the wall of temple written one below the other. Since this temple is very ancient, some symbols from the words were erased. The symbols in the set have equal probability for being in the position of any erased symbol.

Fifa challenged Fafa to calculate the probability that S1 is lexicographically greater than S2. Can you help Fafa with this task?

You know that , i. e. there were m distinct characters in Egyptians' alphabet, in this problem these characters are denoted by integers from 1 to m in alphabet order. A word x is lexicographically greater than a word y of the same length, if the words are same up to some position, and then the word x has a larger character, than the word y.

We can prove that the probability equals to some fraction , where P and Q are coprime integers, and . Print as the answer the value , i. e. such a non-negative integer less than 109 + 7, such that , where means that a and b give the same remainders when divided by m.

Input

The first line contains two integers n and m (1 ≤ n,  m ≤ 105) — the length of each of the two words and the size of the alphabet , respectively.

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ m) — the symbols of S1. If ai = 0, then the symbol at position i was erased.

The third line contains n integers representing S2 with the same format as S1.

Output

Print the value , where P and Q are coprime and is the answer to the problem.

Examples
Input
Copy
1 2
0
1
Output
500000004
Input
Copy
1 2
1
0
Output
0
Input
Copy
7 26
0 15 12 9 13 0 14
11 1 0 13 15 12 0
Output
230769233
Note

In the first sample, the first word can be converted into (1) or (2). The second option is the only one that will make it lexicographically larger than the second word. So, the answer to the problem will be , that is 500000004, because .

In the second example, there is no replacement for the zero in the second word that will make the first one lexicographically larger. So, the answer to the problem is , that is 0.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值