2301: [Haoi2011]problem btime limit:50 Sec Memory limit:256 MB
submit:2170 solved:934
[Submit] [Status] [Discuss] Description for the given n queries, how many pairs (x, y) are asked each time, satisfy A≤x≤b,c≤y≤d, and gcd (x, y) = K,GCD (x, y) functions are greatest common divisor of x and Y.
Input
The first line is an integer n, and the next n rows are five integers per line, representing A, B, C, D, K, respectively.
Output
A total of n rows, one integer per line representing the number of pairs (x, y) that satisfy the requirement
Sample Input2
2 5 1) 5 1
1 5 1) 5 2
Sample Output
14
3
HINT
100% of the data meet: 1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000
Source
[POI2007] Zap has a piece of evidence.
Plus the repulsion.
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath>using namespace std; #define for (i,n) for (int i=1;i<=n ; i++) #define Fork (i,k,n) for (int i=k;i<=n;i++) #define MEM (a) memset (A,0,sizeof (a)); #define MAXN (50000+10) typedef Long Long ll;int p[maxn]={0},tot;bool b[maxn]={0};int mu[maxn]={0},sum[maxn]={0};inline int read () {int X=0,f=1;char ch= GetChar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} void Make_prime (int n) {tot=0; mu[1]=1; Fork (i,2,n) {if (!b[i]) p[++tot]=i,mu[i]=-1; for (J,tot) {if (i*p[j]>n) break;b[i*p[j]]=1;if (i%p[j]==0) {mu[i*p[j]]=0; Mu[i*p[j]]=-mu[i]; }}sum[0]=0; for (I,n) sum[i]=sum[i-1]+mu[i];} int N,m,d;int calc (int n,int m) {int ans=0;if (n>m) Swap (n,m), for (int i=1,last=1;i<=n;i=last+1) {last=min (n/(n/i), m/(m/i)); ans+= (Sum[last]-sum[i-1]) * (n/i) * (m/i);} return ans;} int main () {//freopen ("bzoj2301.in", "R", stdin); MEM (P) mem (b) Mem (MU) mem (sum) int N = 50000;make_prime (n); int T; T=read (); while (t--) {int a=read (), B=read (), C=read (), D=read (), K=read (); printf ("%d\n", Calc (b/k,d/k)-calc ((A-1)/k,d/k)-Calc (b/k, (c-1)/k) + Calc ((A-1)/k, (c-1)/k));} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 2301 ([Haoi2011]problem B-mobius inversion)