首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >HDU 3388 Coprime(容斥原理+二分)

HDU 3388 Coprime(容斥原理+二分)

作者头像
ShenduCC
发布于 2018-04-26 09:11:07
发布于 2018-04-26 09:11:07
62600
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

Coprime

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 849    Accepted Submission(s): 232

Problem Description

Please write a program to calculate the k-th positive integer that is coprime with m and n simultaneously. A is coprime with B when their greatest common divisor is 1.

Input

The first line contains one integer T representing the number of test cases. For each case, there's one line containing three integers m, n and k (0 < m, n, k <= 10^9).

Output

For each test case, in one line print the case number and the k-th positive integer that is coprime with m and n. Please follow the format of the sample output.

Sample Input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
3
6 9 1
6 9 2
6 9 3

Sample Output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Case 1: 1
Case 2: 5
Case 3: 7
这里有两个数n,m;这里用容斥原理同样可以求在1到任意范围内,和n,m两个数互质的个数。把n,m分别分解质因数,然后把相同的合并和求一个数的本质没什么区别,套模版。然后就要去找最小的数x使得1到x与n,m互质的个数等于k,x就是答案。用二分去找n,上限直接设成1<<62#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>

using namespace std;
typedef long long int LL;
const LL INF=(LL)1<<62;
#define MAX 1000000
bool check[MAX+5];
LL prime[MAX+5];
LL sprime[MAX+5];
LL q[MAX+5];
LL n,m,k,cnt;
void eular()//线性筛
{
    memset(check,false,sizeof(check));
    int tot=0;
    for(int i=2;i<=MAX+5;i++)
    {
        if(!check[i])
            prime[tot++]=i;
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>MAX+5) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}
void Divide(LL n,LL m)
{
    cnt=0;
    LL t=(LL)sqrt(1.0*n);
    for(LL i=0;prime[i]<=t;i++)
    {
        if(n%prime[i]==0)
        {
            sprime[cnt++]=prime[i];
            while(n%prime[i]==0)
                n/=prime[i];
        }
    }
    if(n>1)
        sprime[cnt++]=n;
     t=(LL)sqrt(1.0*m);
    for(LL i=0;prime[i]<=t;i++)
    {
        if(m%prime[i]==0)
        {
            sprime[cnt++]=prime[i];
            while(m%prime[i]==0)
                m/=prime[i];
        }
    }
    if(m>1)
        sprime[cnt++]=m;
}
LL Ex(LL n)//容斥原理
{
    LL sum=0,t=1;
    q[0]=-1;
    for(LL i=0;i<cnt;i++)
    {
        LL x=t;
        for(LL j=0;j<x;j++)
        {
            q[t]=q[j]*sprime[i]*(-1);
            t++;
        }
    }
    for(LL i=1;i<t;i++)
        sum+=n/q[i];
    return sum;
}
LL Binary()
{
    LL l=1,r=INF;
    LL ans,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if((mid-Ex(mid))>=k)
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    eular();
    while(t--)
    {
        scanf("%lld%lld%lld",&m,&n,&k);
        if(n==1&&m==1)
        {
            printf("Case %d: %lld\n",++cas,k);
            continue;
        }
        Divide(n,m);
        sort(sprime,sprime+cnt);
        int cot=1;
        for(LL i=1;i<cnt;i++)
        {
            if(sprime[i]!=sprime[i-1])
            {
                sprime[cot++]=sprime[i];
            }
        }
        cnt=cot;
        printf("Case %d: %lld\n",++cas,Binary());

    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9..
ShenduCC
2018/04/26
6770
HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9046    Accepted Submission(s): 3351 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)
ShenduCC
2018/04/26
5160
HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2494    Accepted Submission(s): 775 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), and
ShenduCC
2018/04/26
6180
HDU 5634 Rikka with Phi (线段树)
Problem Description Rikka and Yuta are interested in Phi function (which is known as Euler's totient function). Yuta gives Rikka an array  of positive integers, then Yuta makes  queries.   There are three types of queries:  Change  into ,
ShenduCC
2018/04/27
5900
HDU-4407-Sum(容斥原理)「建议收藏」
XXX is puzzled with the question below:
全栈程序员站长
2022/07/08
2060
【HDU4947】GCD Array (莫比乌斯反演+树状数组)
BUPT2017 wintertraining(15) #5H HDU- 4947 题意 题解 官方题解: 代码 #include<cstdio> #include<cstring> #include
饶文津
2020/06/02
3220
【HDU4947】GCD Array (莫比乌斯反演+树状数组)
数学--数论--HDU6919 Senior PanⅡ【2017多校第九场】
给出一个区间[L,R][L,R],问该区间中所有以KK作为最小因子(大于11的)的数字之和
风骨散人Chiam
2020/10/28
3090
2019 HDU 多校赛第三场 HDU 6609 Find the answer(树状数组+二分答案 查询前k小)
题意: 多组询问,n个人,值m, 接下来n个值,要求当前值加上前面尽量多的值之和小于等于 m ,问前面要去掉几个值
用户2965768
2019/08/01
4360
召唤师·卡尔(Polya定理)- HDU 3923
Polya定理是为了解决环状组合计数的问题,比如,对于一个有5颗珠子的环形手链,给你2种颜色对珠子上色,能够得到多少种不同的手链呢?
ACM算法日常
2018/08/07
1.1K0
召唤师·卡尔(Polya定理)- HDU 3923
HDU-5514-Frogs
该文是有关ACM模板题目的解题报告,题目为HDU-5514-Frogs,作者通过编写代码,分两步解决了该问题。第一步通过容斥原理求解了该问题,第二步通过欧拉函数求解了该问题。
f_zyj
2018/01/09
5850
HDU-5514-Frogs
HDU 1796 How many integers can you find(容斥原理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6434    Accepted Submission(s): 1849 Problem Description   Now you get a number N, and a M-integers set, you should
ShenduCC
2018/04/26
6980
PAT (Basic Level) Practice (中文)1025 反转链表 (25 分)
给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。
glm233
2020/09/28
4010
How many integers can you find(容斥原理) - HDU 1796
看这题之前先复习一下容斥原理,不然肯定看不懂,呃,如果第一次接触容斥原理的题,可能弄懂了容斥原理你还是看不懂代码,是的,等会你就知道了。
ACM算法日常
2018/08/07
7760
CodeFores 665D Simple Subset(贪心)
D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i,  j) (1  ≤ 
ShenduCC
2018/04/26
4890
ZOJ 3661 Palindromic Substring(回文树)
Palindromic Substring ---- Time Limit: 10 Seconds      Memory Limit: 65536 KB ---- In the kingdom of string, people like palindromic strings very much. They like only palindromic strings and dislike all other strings. There is a unified formula to calculat
ShenduCC
2018/04/26
4840
【HDU 6036】Division Game (NTT+数学)
题意 屏幕快照 2020-06-02 下午3.40.23.png 题解 屏幕快照 2020-06-02 下午3.40.36.png 代码 #include <bits/stdc++.h> #define rep(i,l,r) for(int i=l,ed=r;i<ed;++i) #define mem(a,b) memset(a,b,sizeof(a)) typedef long long ll; using namespace std; const ll P = (235 << 22) + 1; cons
饶文津
2020/06/02
4030
HDU 5658 CA Loves Palindromic(回文树)
CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 301    Accepted Submission(s): 131 Problem Description CA loves strings, especially loves the palindrome strings. One day h
ShenduCC
2018/04/26
6930
Day2下午解题报告
预计分数:100+100+30=230 实际分数:100+100+30=230 人品爆发&&智商爆发&&手感爆发 T3数据好水,,要是把数组开大一点的话还能多得10分,,, T1洗澡 原题,不多说了。。 当时在北京花了接近一个小时才A.. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=1e6; 7 cons
attack
2018/04/11
5840
#628 DIV2 题解
组样例,每组给一个和个数 。将同一个序列重复次得到一个新序列,问可以从新序列中严格最长上升子序列长度为多少。
ACM算法日常
2020/04/08
2660
#628 DIV2 题解
HDU 3333 Turing Tree (线段树)
Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4768    Accepted Submission(s): 1686 Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems ab
ShenduCC
2018/04/27
5570
相关推荐
POJ 2773 Happy 2006(容斥原理+二分)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档