1
#define _CRT_SECURE_NO_WARNINGS 1//jerry99的数列 2 #include<bits/stdc++.h> 3 int prime[40000] = { 0 }; 4 int pri[6000] = { 0 }; 5 int nums[3000] = { 0 }; 6 int mem[3000] = { 0 }; 7 int jiecheng[40000*1000] = { 0 }; 8 void pre() {//标记质数1~50000 9 prime[0] = prime[1] = 1; 10 for (int i = 2; i < 40000; i++) { 11 for (int j = 2 * i; j < 40000; j += i) { 12 prime[j] = 1; 13 } 14 } 15 int k = 0; 16 for (int i = 0; i < 40000; i++) { 17 if (!prime[i]) 18 pri[k++] = i;//存放在num数组里备用 19 } 20 } 21 22 23 int div2(int n) { 24 memset(mem, 0, 6000); 25 int k = 0, d, num = 0, origin = n; 26 for (int i = 0; n != 1; i++) { 27 d = pri[i]; 28 while (n % d == 0) { 29 mem[k]++; 30 n /= d; 31 } 32 k++; 33 if (k >= 5133&&n>50000) return 1; 34 } 35 return 0; 36 } 37 int main() { 38 pre();//筛出质数 39 //提前分解好1~20000阶乘质因数 40 41 42 int t, n, m; 43 for (n = 1; n < 40000; n++) { 44 int k = 0, d, num = 0, origin = n; 45 for (int i = 0; pri[i]<=n; i++) { 46 d = pri[i]; 47 while (origin != 1&&origin%d==0) { 48 nums[k]++; 49 origin /= d; 50 } 51 k++; 52 } 53 for (int p = 0; p < k; p++) { 54 *(jiecheng+1000*n+p) += (*(jiecheng+1000*(n-1)+p)+ nums[p]);//递推地计算质因数分解结果 55 //printf("%d ", jiecheng[n][p]);//数组一维化 56 }//printf("\n"); 57 memset(nums, 0, 3000*4); 58 } 59 for (int i = 1; i < 10; i++) { 60 for (int j = 0; j < 10; j++) { 61 printf("%d ", jiecheng[i][j]); 62 }printf("\n"); 63 } 64 scanf("%d", &t); 65 int judge; 66 while (t > 0) { 67 memset(mem, 0, 3000*4); 68 scanf("%d%d", &n, &m);//n分母,m分子 69 judge = div2(n);//把分母质因数分解 70 71 if (judge&&n>40000) {//如果n分解后的质因数是个超级大(>50000)的质数 72 if (m < n) printf("0\n"); 73 else { 74 printf("%d\n", m - n + 1); 75 } 76 } 77 else {//小于50000的质数或者不是质数的10^9以内的 78 int cnt, j = 0; 79 for (cnt = 1; cnt < n;) { 80 //printf("%d ", *(jiecheng + 1000 * cnt + j)); 81 while (mem[j] > * (jiecheng + 1000 * cnt + j)) { 82 cnt++;//每一位幂次进行比较 83 } 84 85 //printf("%d ", *(jiecheng + 1000 * cnt + j)); 86 if (mem[j] <= *(jiecheng + 1000 * cnt + j)) { 87 j++;//比较完一个每次进入下一个幂次 88 } 89 if (!mem[j] && j == n) { 90 break; 91 } 92 93 94 } 95 /*for (int i = 1; i < n; i++) { 96 for (int j =0 ; j < 10; j++) { 97 printf("%d ", *(jiecheng +1000 * i + j)); 98 } 99 }*/ 100 //printf("比%d大的最小阶乘是 %d !\n", n, cnt); 101 if (cnt > m + 1) printf("0\n"); 102 else printf("%d\n", m - cnt + 1); 103 t--; 104 } 105 } 106 return 0; 107 } 108