标签:blog io on 2014 log amp ef as file
/* 题意:给你二个区间[a,b]和[c,d] 分别从中选一个数x和y使的(x+y)%p=m; 可以这样来求,先求出(0->b和0->d区间段的值)-(区间0->a-1和0->d的值)-(区间0->b和0->c-1的值)+(0->a-1和0->c-1)的值 而且发现有这样的规律对于连个区间的末端值a和b来说 当在0->a这个区间内有一个单调性,a->b-1这个区间内有单调性,b-(a+b-m)/p*p这个区间内有单调性 直接用等差数列计算即可 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #define ll __int64 ll p,m; ll f(ll aa,ll bb) { if(aa<0||bb<0)return 0; ll i; if(aa>bb) { i=aa; aa=bb; bb=i; } i=0; ll j=0; ll sum=0; i=(aa-m)/p; if(i>=0&&aa>=m) { sum+=(m+1+i*p+m+1)*(i+1)/2; i++; } else i=0; // printf("i=%I64d\n",sum); j=(bb-m)/p; if((bb-m)%p==0)j--; if(j>=i&&bb>=m) { sum+=(aa+1)*(j-i+1); j++; } else j=i; // printf("j=%I64d\n",sum); ll k=(aa+bb-m)/p; if(k>=j&&aa+bb>=m) { // printf("%I64d %I64d\n",aa,bb); sum+=(aa+bb+1)*(k-j+1); // printf("k=%I64d\n",sum); sum-=(j*p+m+k*p+m)*(k-j+1)/2; } // printf("%I64d\n",sum); return sum; } ll gcd(ll a,ll b) { if(b==0)return a; return gcd(b,a%b); } int main() { ll t,a,b,c,d,aa,bb,k=0; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m); aa=f(b,d)-f(a-1,d)-f(b,c-1)+f(a-1,c-1); if(aa==0) { printf("Case #%I64d: 0/1\n",++k); continue; } bb=gcd(aa,(b-a+1)*(d-c+1)); printf("Case #%I64d: %I64d/%I64d\n",++k,aa/bb,(b-a+1)*(d-c+1)/bb); } return 0; }
标签:blog io on 2014 log amp ef as file
原文地址:http://blog.csdn.net/u011483306/article/details/40896669