Topic Link
Subject to the effect
Individual flowers n (cyclic) to give the number of flowers each section may, if the product of the number of adjacent two flowers is a multiple of p, each of the two obtained 1000,
Demand expectations for all wages. If the answer is a / b is written as a * inv [b]
Topic ideas
1: How to judge [L, R] middle how many multiples of p, but not very easy to want to think about or know is actually R / p- (L-1) / p
2: The subject is not necessary to obtain a final probability, and so with the least common multiple operating directly inverse to solving
3: n is 1 and 2 will have to judge Patent, Laid attention judgment can not a [n + 1] = 0, as this is the inverse operation, directly reduce the cycle judged Laid
Code
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=1e6+5;
int n,p,l,r;
ll a[maxn],ans;
int extgcd(int a,int b,int &x,int &y){//扩欧
int d=a;
if(b!=0){
extgcd(b,a%b,y,x);
y-=(a/b)*x;
}else{
x=1,y=0;
}
return d;
}
int main(){
scanf("%d %d",&n,&p);
for(int i=1;i<=n;i++){
scanf("%d %d",&l,&r);
int x,y;
extgcd(r-l+1,mod,x,y);
x=(mod+x%mod)%mod;
a[i]=(((long long)(r/p-(l-1)/p))*x)%mod;
}
a[n+1]=a[1];//选择n和1
if(n==1&&n==2){//特判必须这么特判不能给a[n+1]赋值为0
n--;
}
for(int i=1;i<=n;i++){
ans=((mod+1-(mod+1-a[i])*(mod+1-a[i+1])%mod)%mod+ans)%mod;//这个我觉得最好就是先写出平常的式子,然后再加上mod
}
ans=ans*2000%mod;
printf("%lld",ans);
return 0;
}