GCD HDU - 1695
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GCD HDU - 1695相关的知识,希望对你有一定的参考价值。
题意:
给出a,b,c,d,k,求出a<=x<=b, c<=y<=d 且gcd(x,y) == k 的(x,y)的对数。
求的是不同数量对的总数
题解:
和这个题一样P3455 [POI2007]ZAP-Queries,但是本题要求求不同数量对的总数,所以最后的结果要减去重复值
如果是莫比乌斯+分块做法,对于区间[1,b],[1,d],b<d,重复部分是[1,b]部分,所以减去solve(b,b)/2
如果是容斥做法,对于每个i∈[1,b],求[1,d]中互质的数量,那我们减去i与[1,i]中互质的数量
详细看代码
代码:
莫比乌斯+分块
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
read(Ar...);
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#else
startTime = clock ();
freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#else
endTime= clock();
printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn=2e6+9;
int prime[maxn];
int mu[maxn];
int sum[maxn];
int vis[maxn];
int cnt=0;
void get_mu(int N){
mu[1]=1;
vis[1]=vis[0]=1;
for(int i=2;i<=N;i++){
if(!vis[i]){
prime[++cnt]=i;
mu[i]=-1;
}
for(int j=1;j<=cnt&&i*prime[j]<=N;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0)break;
mu[i*prime[j]]=-mu[i];
}
}
for(int i=1;i<=N;i++){
sum[i]=sum[i-1]+mu[i];
}
}
ll solve(int a,int b,int k){
a/=k;
b/=k;
int minn=min(a,b);
ll ans=0;
for(int l=1,r;l<=minn;l=r+1){
r=min(a/(a/l),b/(b/l));
ans+=1ll*(sum[r]-sum[l-1])*(a/l)*(b/l);
}
return ans;
}
int main()
{
//rd_test();
get_mu(1000000);
int t;
read(t);
int cas=0;
while(t--){
int a,b,c,d,k;
read(a,b,c,d,k);
if(k==0){
printf("Case %d: 0\\n",++cas);
continue;
}
if(b>d)swap(b,d);
// printf("%d %d %d %d\\n",a,b,c,d);
printf("Case %d: %lld\\n",++cas,solve(b,d,k)-solve(b,b,k)/2);
}
//Time_test();
}
容斥
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
read(Ar...);
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#else
startTime = clock ();
freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#else
endTime= clock();
printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn=3e5+9;
int prime[maxn];
int cnt=0;
void divide(int n){
cnt=0;
for(int i=2;i*i<=n;i++){
if(n%i==0){
prime[cnt++]=i;
while(n%i==0)n/=i;
}
}
if(n!=1)prime[cnt++]=n;
}
int solve(int S){
int ans=0;
for(int i=1;i<(1<<cnt);i++){
int tmp=1;
int num=0;
for(int j=0;j<cnt;j++){
if(i&(1<<j)){
tmp*=prime[j];
num++;
}
}
if(num&1)ans+=S/tmp;
else ans-=S/tmp;
}
return S-ans;
}
int main()
{
//rd_test();
int t;
read(t);
int cas=0;
while(t--){
int a,b,c,d,k;
read(a,b,c,d,k);
if(k==0){
printf("Case %d: 0\\n",++cas);
continue;
}
if(b>d)swap(b,d);
b/=k;
d/=k;
ll ans=0;
for(int i=1;i<=b;i++){
divide(i);
ans+=solve(d)-solve(i-1);
}
printf("Case %d: %lld\\n",++cas,ans);
}
return 0;
//Time_test();
}
以上是关于GCD HDU - 1695的主要内容,如果未能解决你的问题,请参考以下文章