Preprocessing is performed first, and factorization is decomposed for each number.
Then because if gcd (x, y) ==z, then gcd (x/z,y/z) ==1, and because it is not a multiple of Z is certainly not, so not a multiple of z can be removed directly, so as long as the B and D divided by K, and then converted to the two range of coprime logarithm. At this time can enumerate 1~b, and then use the tolerant principle to find 1~d in the range of the number of coprime with the enumerator, in order to avoid repetition, as long as the size of the relationship can be limited, see the code.
The code is as follows:
#include <iostream> #include <string.h> #include <math.h> #include <queue> #include < algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>using namespace std; #define LL __int64const int mod=1e9+7;const int inf=0x3f3f3f3f; LL ans; ll A, B, C, d;vector<int>vec[110000];void Dfs (ll i, int cur, int cnt, LL tmp) {tmp*= (LL) vec[i][cur]; if (cnt&1) {ans+= (LL) min (d,i)/tmp; } else {ans-= (LL) min (d,i)/tmp; } for (int j=cur+1; j<vec[i].size (); j + +) {DFS (i,j,cnt+1,tmp); }}void init () {int I, j, X, CNT; for (I=1; i<=100000; i++) {x=i; cnt=0; for (j=2; j*j<=x; J + +) {if (x%j==0) {vec[i].push_back (j); while (x%j==0) x/=j; }} if (x>1) Vec[i].push_back (x); }}int Main () {int T, I, J, num=0; LL sum, K; Init (); scanf ("%d", &t); while (t--) {scanf ("%i64d%i64d%i64d%i64d%i64d", &a,&b,&c,&d,&k); num++; if (!k) {printf ("Case%d:0\n", num); Continue; } b/=k; D/=k; if (b<d) swap (B,D); sum=d* (d+1)/2+ (b-d) *d; ans=0; For (I=1, i<=b; i++) {for (j=0; J<vec[i].size (); j + +) {DFS (i,j , 1, 1); }//printf ("%i64d\n", ans); }//printf ("%i64d%i64d\n", Sum,ans); printf ("Case%d:%i64d\n", Num,sum-ans); } return 0;}
HDU 1695 GCD (Tolerant principle + mass factor decomposition)