2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 436  Solved: 187
[Submit][Status]

2

2 5 1 5 1

1 5 1 5 2

14

3

HINT

100%的数据满足：1≤n≤50000，1≤a≤b≤50000，1≤c≤d≤50000，1≤k≤50000

Source

[Submit][Status]

HOME Back

http://www.cnblogs.com/zhsl/p/3269288.html

http://wenku.baidu.com/view/fbe263d384254b35eefd34eb.html

/* ***********************************************
Author        :kuangbin
Created Time  :2013/8/21 20:19:04
File Name     :F:\2013ACM练习\专题学习\数学\莫比乌斯反演\BZOJ2301.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = 100000;
bool check[MAXN+10];
int prime[MAXN+10];
int mu[MAXN+10];
void Moblus()
{
memset(check,false,sizeof(check));
mu[1] = 1;
int tot = 0;
for(int i = 2; i <= MAXN; i++)
{
if( !check[i] )
{
prime[tot++] = i;
mu[i] = -1;
}
for(int j = 0; j < tot; j ++)
{
if( i * prime[j] > MAXN) break;
check[i * prime[j]] = true;
if( i % prime[j] == 0)
{
mu[i * prime[j]] = 0;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
}
}
int sum[MAXN+10];
//找[1,n],[1,m]内互质的数的对数
long long solve(int n,int m)
{
long long ans = 0;
if(n > m)swap(n,m);
for(int i = 1, la = 0; i <= n; i = la+1)
{
la = min(n/(n/i),m/(m/i));
ans += (long long)(sum[la] - sum[i-1])*(n/i)*(m/i);
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
Moblus();
sum[0] = 0;
for(int i = 1;i <= MAXN;i++)
sum[i] = sum[i-1] + mu[i];
int a,b,c,d,k;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
long long ans = solve(b/k,d/k) - solve((a-1)/k,d/k) - solve(b/k,(c-1)/k) + solve((a-1)/k,(c-1)/k);
printf("%lld\n",ans);
}
return 0;
}

posted on 2013-08-21 21:12  kuangbin  阅读(4455)  评论(0编辑  收藏

• 随笔 - 940
• 文章 - 0
• 评论 - 595
• 引用 - 0
JAVASCRIPT: