hdu1695

求1<=i<=n&&1<=j<=m,gcd(i,j)=k的(i,j)的对数

最后的结果f(k)=Σ(1<=x<=n/k)mu[x]*(n/(x*k))*(m/(x*k))

遍历的复杂度是O(n/k),按理来说是会t的,但是这题过了,更好的办法是用分块降低到O(sqrt(n/k))

详细介绍请看:链接

这题要(i,j)和(j,i)算重复的,所以要减去

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int mu[N],prime[N],sum[N];
bool mark[N];
void init()
{
mu[]=;
int cnt=;
for(int i=;i<N;i++)
{
if(!mark[i])prime[++cnt]=i,mu[i]=-;
for(int j=;j<=cnt;j++)
{
int t=i*prime[j];
if(t>N)break;
mark[t]=;
if(i%prime[j]==){mu[t]=;break;}
else mu[t]=-mu[i];
}
}
for(int i=;i<N;i++)sum[i]=sum[i-]+mu[i];
}
int main()
{
init();
int t,cnt=;
scanf("%d",&t);
while(t--)
{
ll a,b,c,d,k;
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
if(!k)
{
printf("Case %d: 0\n",++cnt);
continue;
}
if(b>d)swap(b,d);
b/=k,d/=k;
ll ans=,ans1=;
for(ll i=,last=;i<=b;i=last+)
{
last=min(b/(b/i),d/(d/i));
ans+=(ll)(sum[last]-sum[i-])*(b/i)*(d/i);
}
for(ll i=,last=;i<=b;i=last+)
{
last=b/(b/i);
ans1+=(ll)(sum[last]-sum[i-])*(b/i)*(b/i);
}
printf("Case %d: %lld\n",++cnt,ans-ans1/);
}
return ;
}
/******************** ********************/
04-15 07:40