bzoj2301: [HAOI2011]Problem b
莫比乌斯反演学姿势
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define clr(x,c) memset(x,c,sizeof(x)) #define ll long long int read(){ int x=0;char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-'0',c=getchar(); return x; } const int nmax=50005; int mo[nmax],prime[nmax],cnt=0;bool vis[nmax]; void init(){ mo[1]=1; rep(i,2,nmax-1){ if(!vis[i]) prime[++cnt]=i,mo[i]=-1; rep(j,1,cnt){ if(i*prime[j]>=nmax) break; vis[i*prime[j]]=1; if(i%prime[j]==0){ mo[i*prime[j]]=0;break; } mo[i*prime[j]]=-mo[i]; } } rep(i,2,nmax-1) mo[i]+=mo[i-1]; } /*void init(){ mo[1]=1; rep(i,2,nmax-1){ if(!vis[i]) prime[++cnt]=i,mo[i]=-1; rep(j,1,cnt){ int tmp=prime[j]; if(i*tmp>=nmax) break; vis[i*tmp]=1; if(i%tmp==0) { mo[i*tmp]=0;break; } mo[i*tmp]=-mo[i]; } } rep(i,2,nmax-1) mo[i]+=mo[i-1]; }*/ ll cal(int n,int m,int k){ ll ans=0;n/=k;m/=k; int last,t=min(n,m); for(int i=1;i<=t;i=last+1){ last=min(n/(n/i),m/(m/i)); ans+=(ll)(m/i)*(n/i)*(mo[last]-mo[i-1]); } return ans; } int main(){ init(); int n=read(),a,b,c,d,k; rep(i,1,n){ a=read(),b=read(),c=read(),d=read();k=read(); printf("%lld\n",cal(b,d,k)-cal(b,c-1,k)-cal(a-1,d,k)+cal(a-1,c-1,k)); } return 0; }
2301: [HAOI2011]Problem b
Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 3780 Solved: 1684
[Submit][Status][Discuss]
Description
对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。
Input
第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k
Output
共n行,每行一个整数表示满足要求的数对(x,y)的个数
Sample Input
2
2 5 1 5 1
1 5 1 5 2
2 5 1 5 1
1 5 1 5 2
Sample Output
14
3
HINT
100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000