Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4790
A random number X from [a, B], a random number y from [c, d], p, M given, if (x + y) % P = m indicates that the request is successful.
Idea: [a, B] continuous P count, [c, d] continuous P count, using the combination of the 2 * P numbers, we can find the successful combination of P (not specific), so we can find the number of P cycles in [a, B] X1, [C, d: if the number of P cycles is Y1, the successful combination of them is p * X1 * Y1.
Then we will deal with the boundary problem. First, the number of the numbers of X or Y in the boundary must not exceed P-1 and there are X2 and Y2 respectively, the number of Y2 must be able to find the corresponding successful combination in each cycle, so there will be an extra X2 * Y1 + y2 * X1 number of successful combinations.
Finally, we need to find the corresponding number in the X2, Y2 number, because the X2 + y2 1_m (mod P) is required, so (m-x2) 1_y2 (mod P ), find the overlapping parts.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <cstdlib>#include <queue>#include <stack>#include <vector>#include <ctype.h>#include <algorithm>#include <string>#include <set>#define PI acos(-1.0)#define maxn 10005#define INF 0x7fffffff#define eps 1e-8typedef long long LL;typedef unsigned long long ULL;using namespace std;int main(){ int T; scanf("%d",&T); for(int ii=1; ii<=T; ii++) { LL a,b,c,d,p,m; scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m); LL x=b-a+1,y=d-c+1; LL xx=(b-a+1)/p; LL yy=(d-c+1)/p; LL xxx=x-xx*p; LL yyy=y-yy*p; LL t=xx*yy*p; LL fm=x*y; t+=xxx*yy; t+=yyy*xx; if(xxx!=0&&yyy!=0) { LL head=a%p; LL tail=b%p; LL head1=((m-tail)%p+p)%p; LL tail1=((m-head)%p+p)%p; LL head2=c%p; LL tail2=d%p; //cout<