p3266题解
前期转化,略。
总之,求
显然,去掉不经过直线的限制后答案为
考虑不合法的路径会是怎样,如果记录这条路径经过直线
将显然连续经过同个直线与只接触一下直线可以归为一类,所以把序列再改为
于是我们可以将不合法路径分为两种,一种是先经过
考虑一条不合法路径是如何构建这个序列的。
假设这个路径是先经过
a
ab
aba
abab
ababa
...
发现如果最后一次
于是对于开头是
但是如果直接像刚刚那样,算
为了避免这种情况,我们可以利用先加上
以此往复,发现在加上每一个关于
显然,当
对于
最终答案为
因为每次对称横纵坐标减少量至少为
码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define i8 __int128
void write1(ll x) {if (x < 0) putchar('-'), x = -x;if (x > 9) write1(x / 10);putchar(x % 10 + '0');}
void write(ll x) {static ll sta[35];ll top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) putchar(sta[--top] + 48);}
ll read() {ll x = 0, w = 1;char ch = 0;while (ch < '0' || ch > '9') { if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9') {x = x * 10 + (ch - '0'); ch = getchar(); }return x * w; }
const ll N=3e6+10,mod=1e9+7;
ll jc[N];
inline ll ksm(ll x,ll m){
ll rs=1;while(m){if(m&1)(rs*=x)%=mod;(x*=x)%=mod;m/=2;}return rs;
}
inline ll C(ll n,ll m){
if(n<0||m<0)return 0;
if(m>n)return 0;
if(!m)return 1;
if(n==m)return 1;
return (jc[n]*ksm(jc[m]*jc[n-m]%mod,mod-2))%mod;
}
struct mqm{
ll x,y;
void dc(ll b){
swap(x,y);x-=b;y+=b;
}
};
ll n,m;
int main(){
n=read(),m=read();
mqm yd;yd=(mqm){n+m+1,n};
jc[0]=1;
for(ll i=1;i<=3e6;i++)jc[i]=jc[i-1]*i%mod;
ll as=C(n+m+1+n,n+m+1);
for(mqm jb=(mqm){n+m+1,n};jb.x>=0&&jb.y>=0;){
jb.dc(1);
(as+=mod-C(jb.x+jb.y,jb.x))%=mod;
jb.dc(-m-2);
(as+=C(jb.x+jb.y,jb.x))%=mod;
}
for(mqm jb=(mqm){n+m+1,n};jb.x>=0&&jb.y>=0;){
jb.dc(-m-2);
(as+=mod-C(jb.x+jb.y,jb.x))%=mod;
jb.dc(1);
(as+=C(jb.x+jb.y,jb.x))%=mod;
}
cout<<as;
}