博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1695(欧拉函数 容斥定理)
阅读量:6950 次
发布时间:2019-06-27

本文共 2279 字,大约阅读时间需要 7 分钟。

Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
OutputFor each test case, print the number of choices. Use the format in the example.

 

 

 

 

2 1 3 1 5 1 1 11014 1 14409 9
Sample Output
Case 1: 9 Case 2: 736427
Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).

 

 

Sample Input

 

 

 

 

//分析:题目是要求a<=x<=b,c<=y<=d(其中a,c题目中已经说了必为1),中有多少对(x,y)(无序的)满足GCD(x,y)=k,那么可以转化成求(x,y)分别在区间1<=x<=b/k,1<=y<=d/k中有多少对满足GCD(x,y)=1; 转换b/=k,d/=k,之后,假设b<=d,那么我们可以分两步来求值:

1、求[1,b]与[1,b]之间有多少对数互质,显然就是phi[1]+phi[2]+...phi[b]即可,用线性求欧拉函数即可!

2\求[1,b]与[b+1,d]之间有多少对数互质,使用容斥定理

两步求值之和即为题意所求!

 

#include<iostream>

#include<cstdio>
#include<cstring>
using namespace std;
const int N=100000;
int q[N+5];
long long p[N+5];
int f(int a,int n)
{
    int w[110],l=0;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            w[++l]=i;
            while(n%i==0)n/=i;
        }
        if(n==1)break;
    }
    if(n!=1)w[++l]=n;
    int sum=0;
    for(int i=1;i< (1<<l);i++)
    {
        int flat=0,s=1;
        for(int j=0;j<l;j++)
            if(i&(1<<j))
            {flat=!flat;
             s*=w[j+1];
            }
            s=a/s;
        if(flat)
            sum+=s;
        else
            sum-=s;
    }
    return (a-sum);
}
int main()
{
    memset(q,0,sizeof(q));
    q[0]=q[1]=1;
    int n=0;
    for(int i=2;i<=N;i++)
    {
        if(!q[i])
        {
            for(int j=i;j<=N;j+=i)
            {
                if(!q[j])q[j]=j;
                q[j]=q[j]/i*(i-1);
            }
        }
    }
    p[1]=1;
    for(int i=2;i<=N;i++)
    {
        p[i]=p[i-1]+q[i];
    }
    int m,a,b,c,d,k;
    int t,kase=0;
    scanf("%d",&t);
    while(t--)
    {
        long long ans;
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0||b<k||d<k)
            ans=0;
        else
        {
         b/=k;d/=k;
          if(b>d)swap(b,d);
             ans=0;
            for(int i=b+1;i<=d;i++)
                {int k=f(b,i);
                //cout<<i<<"  * "<<k<<endl;
                 ans+=k;
                }
            ans+=p[b];
        }
           printf("Case %d: %lld\n",++kase,ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/552059511wz/p/6741424.html

你可能感兴趣的文章
Linux 僵尸进程的筛选和查杀
查看>>
mysql linux app
查看>>
DotNetCore学习-3.管道中间件
查看>>
Python基础11_函数名运用,闭包,迭代器
查看>>
java集合框架
查看>>
python之configparse模块
查看>>
CentOS6.2编译安装MySQL5.5.25
查看>>
Nyoj 星际之门(一)(Cayley定理)
查看>>
词法分析程序
查看>>
前端基础之css
查看>>
网址收藏
查看>>
人的成长,注定是一场孤独的旅途 ...(360doc)
查看>>
死锁排查的小窍门 --使用jdk自带管理工具jstack
查看>>
安卓开发者必备的42个链接
查看>>
DeadLine
查看>>
2018-2019 Exp2 后门原理与实践
查看>>
bzoj5137 [Usaco2017 Dec]Standing Out from the Herd
查看>>
Mysql压缩包版zip的安装方法
查看>>
UWP 动画
查看>>
浅析设计模式(二)——工厂方法模式
查看>>