cf935d(概率dp+逆元)

这个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
#include
#include
#include
#include
#include
#include
#include
#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<>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.


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