X problems

**Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)**

Total submission (s): 3295 accepted submission (s): 1068

Problem description calculates the number of X values in a positive integer less than or equal to N. x MOD a [0] = B [0], x mod a [1] = B [1], x mod a [2] = B [2],…, X mod a [I] = B [I],… (0 <A [I] <= 10 ).

The first line of input data is a positive integer T, indicating that there are T groups of test data. The first behavior of each group of test data is two positive integers n, m (0 <n <= 1000,000,000, 0 <m <= 10), indicating that X is less than or equal to N, the arrays A and B have m elements. In the next two rows, each row has M positive integers, which are elements in A and B respectively.

Output corresponds to each input group, and a positive integer is output in an independent row, indicating the number of X that meets the conditions.

Sample Input

310 31 2 30 1 2100 73 4 5 6 7 8 91 2 3 4 5 6 710000 101 2 3 4 5 6 7 8 9 100 1 2 3 4 5 6 7 8 9

Sample output

103

The number of M groups. Each group represents X % AI = Bi. Returns the number of X in the N range. Because all AI is not of mutual quality, we cannot directly use the Chinese residue theorem, but we can use the equation (for a * x + B * Y = C, X increases by B/GAD (a, B) each time, and then you only need to determine the number of occurrences within n.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define LL __int64LL t , n , m , d , x , y , i , bb , aa , flag ;void gcd(LL a,LL b){ if(b == 0) { d = a ; x = 1 ; y = 0 ; } else { gcd(b,a%b); swap(x,y); x = -x ; y = -y ; y += (a/b)*x ; } return ;}LL a[30] , b[30] ;int main(){ scanf("%I64d", &t); while(t--) { scanf("%I64d %I64d", &n, &m); for(i = 0 ; i < m ; i++) scanf("%I64d", &a[i]); for(i = 0 ; i < m ; i++) scanf("%I64d", &b[i]); aa = a[0] ; bb = b[0] ; flag = 1 ; for(i = 1 ; i < m ; i++) { gcd(aa,a[i]); if( (b[i]-bb)%d != 0 ) flag = 0 ; if( flag ) { x = (b[i]-bb)/d*x ; y = a[i] / d ; x = ( x%y + y )%y ; bb = bb + x * aa ; aa = aa*a[i]/d ; } } gcd(1,aa); if( bb%d != 0 ) flag = 0 ; if( flag ) { x = ( bb/d )*x ; y = aa / d ; x = (x % y + y) % y ; } if( flag == 0 || x > n ) printf("0\n"); else { if( !x ) printf("%I64d\n", (n-x)/y ); else printf("%I64d\n", (n-x)/y+1 ); } } return 0;}