Codeforces 483B - Friends and Presents - [二分]

题目链接:http://codeforces.com/contest/483


A - Counterexample - [简单构造题]

Your friend has recently learned about coprime numbers. A pair of numbers $(a,?b)$ is called coprime if the maximum number that divides both $a$ and $b$ is equal to one.

Your friend often comes up with different statements. He has recently supposed that if the pair $(a,?b)$ is coprime and the pair $(b,?c)$ is coprime, then the pair $(a,?c)$ is coprime.

You want to find a counterexample for your friend‘s statement. Therefore, your task is to find three distinct numbers $(a,?b,?c)$, for which the statement is false, and the numbers meet the condition $l?\le?a?<?b?<?c?\le?r$.

More specifically, you need to find three numbers $(a,?b,?c)$, such that $l?\le?a?<?b?<?c?le?r$, pairs $(a,?b)$ and $(b,?c)$ are coprime, and pair $(a,?c)$ is not coprime.

Input
The single line contains two positive space-separated integers l, r (1?≤?l?≤?r?≤?1018; r?-?l?≤?50).

Output
Print three positive space-separated integers a, b, c — three distinct numbers (a,?b,?c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.

If the counterexample does not exist, print the single number -1.

Examples
Input
2 4
Output
2 3 4
Input
10 11
Output
-1
Input
900000000000000009 900000000000000029
Output
900000000000000009 900000000000000010 900000000000000021
Note
In the first sample pair (2,?4) is not coprime and pairs (2,?3) and (3,?4) are.

In the second sample you cannot form a group of three distinct integers, so the answer is -1.

In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.

题意:

在给定的 $[l,r]$ 里找到三个数字 $a,b,c$,满足 $gcd(a,b) = 1, gcd(b,c) = 1, gcd(a,c) > 1$。

题解:

相邻两个整数必然互质,相邻两个奇数必然互质。因此只能找连续的三个为偶奇偶的数。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll l,r;
int main()
{
    cin>>l>>r;
    ll a=(l%2==0)?l:l+1;
    ll b=a+1, c=b+1;
    if(c<=r) printf("%lld %lld %lld\n",a,b,c);
    else printf("-1\n");
}

B - Friends and Presents - [二分]

You have two friends. You want to present each of them several positive integers. You want to present $cnt_1$ numbers to the first friend and $cnt_2$ numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.

In addition, the first friend does not like the numbers that are divisible without remainder by prime number $x$. The second one does not like the numbers that are divisible without remainder by prime number $y$. Of course, you‘re not going to present your friends numbers they don‘t like.

Your task is to find such minimum number $v$, that you can form presents using numbers from a set $1,?2,?...,?v$. Of course you may choose not to present some numbers at all.

A positive integer number greater than $1$ is called prime if it has no positive divisors other than $1$ and itself.

Input
The only line contains four positive integers $cnt_1, cnt_2, x, y (1?\le?cnt1,?cnt2?<?10^9; cnt1?+?cnt2?\le?10^9; 2?\le?x?<?y?\le?3·10^4)$ — the numbers that are described in the statement. It is guaranteed that numbers $x, y$ are prime.

Output
Print a single integer — the answer to the problem.

Examples
input
3 1 2 3
output
5
input
1 3 2 3
output
4

Note
In the first sample you give the set of numbers {1,?3,?5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1,?3,?5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.

In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1,?2,?4} to the second friend. Thus, the answer to the problem is 4.

题意:

现在要给两个人赠送一些正整数,要给第一个人送 $cnt_1$ 个正整数,要给第二个人送 $cnt_2$ 个正整数,且第一个人不想要能被 $x$ 整除的数,第二个人不想要能被 $y$ 整除的数。

现在你要求出最小的正整数 $v$,意味着你送出的正整数全部属于 $1 \sim v$。

题解:

二分倒是不难想到,就是算怎么给两人整数不太好想。

举个栗子:假设我现在 $v=7,x=2,y=3,cnt_1=3,cnt_2=3$,显然此时能给第一个人的数字有 $1,3,5,7$,能给第二个人的数字有 $1,2,4,5,7$;

那么我们知道,为了尽量使得 $v$ 小,我们应该尽量把第二个人不要的数字塞给第一个人,比如第二个人是不要 $3$ 的,但第一个人要,所以可以塞给他;同理,第一个人不要 $2,4$,但是第二个人要,可以塞给他;

假设 $1 \sim v$ 中能被 $x$ 整除的数有 $cnt_x$ 个,能被 $y$ 整除的数有 $cnt_y$ 个,能被 $xy$ 整除的数有 $cnt_xy$ 个;因此,第二个人不要而第一个人要的数字,其数量为 $cnt_y - cnt_xy$,第一个人不要而第二个人要的数字,其数量为 $cnt_x - cnt_xy$;

那么剩下来再要送出去的数字,就既不能被 $x$ 整除,也不能被 $y$ 整除了,如果这些数够分,那么 $v$ 就是可行的,不够分的话 $v$ 就不可行。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int cnt1,cnt2,x,y;

int judge(int v)
{
    int cnt_x=v/x, cnt_y=v/y, cnt_xy=v/(x*y);
    int rest1=max(cnt1-(cnt_y-cnt_xy),0);
    int rest2=max(cnt2-(cnt_x-cnt_xy),0);
    return rest1+rest2<=v-cnt_x-cnt_y+cnt_xy;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin>>cnt1>>cnt2>>x>>y;
    int l=1, r=2*(cnt1+cnt2);
    while(l<r)
    {
        int mid=l+(r-l)/2;
        if(judge(mid)) r=mid;
        else l=mid+1;
    }
    cout<<l<<‘\n‘;
}


C - Diverse Permutation - []

原文地址:https://www.cnblogs.com/dilthey/p/9951690.html

时间: 2024-11-05 12:12:52

Codeforces 483B - Friends and Presents - [二分]的相关文章

Codeforces 483B Friends and Presents(二分+数论)

题目链接:Codeforces 483B Friends and Presents 题目大意:要将1~v直间的数分配到两个集合中,第一个集合需要cnt1个数,第二个需要cnt2个数,第一个集合中的数 不能是x的倍数,同理第二个集合不能是y的倍数,两集合元素不能相同,问说v最小可以为多少. 解题思路:这题比第三题要难,想了有一会.二分答案,v,然后判断. 判断的时候只要分别判断集合一,二个数是否满足,但是因为有些数可以被分到两个集合,所以要判断总的可分配个数 是否满足大于cnt1+cnt2,计算总

Codeforces 483B - Friends and Presents(二分+容斥)

483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset((a),(b),sizeof(a)) const ll INF=1e18; ll c1,c2,

CodeForces 483B Friends and Presents

Friends and Presents Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 483B Description You have two friends. You want to present each of them several positive integers. You want to presen

codeforces B. Friends and Presents(二分+容斥)

题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除! 并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值! 思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数 s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数 整除的个数! 那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件! 1 #incl

数学/Codeforces 483b Friends and Presents

1 #include<cstdio> 2 using namespace std; 3 long long m,cnt1,cnt2,x,y; 4 long long gcd(long long a,long long b) 5 { 6 if (b==0) return a; 7 return gcd(b,a % b); 8 } 9 long long lcm(long long a,long long b) 10 { 11 return a*b/gcd(a,b); 12 } 13 bool f

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

Codeforces 8D Two Friends 三分+二分+计算几何

题目链接:点击打开链接 题意:点击打开链接 三分house到shop的距离,二分这条斜边到cinema的距离 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<

Codeforces 479D Long Jumps(贪心+二分)

题目链接:Codeforces 479D Long Jumps 题目大意:valery是个体育老师,现在他要为学生考跳远,女生标准为x,男生为y,现在一个长为L的刻度尺,有N个刻 度,给定N个刻度,现在为说还需要加几个刻度才能测量x,y这两个长度. 解题思路:因为总共就x,y两个长度,所以最多加两个刻度.所以只要判断不加和加一个的情况即可. 先枚举每个刻度a[i],然后用二分查找一下a[i]+x或者a[i]-x刻度存不存在,同理y.如果x和y都通过判断,那么就是不需 要加刻度. 如果只通过x或只

Codeforces 484B Maximum Value(高效+二分)

题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,并且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然后枚举k,每次用二分找到小于k?aj并且最大的ai,维护答案,过程中加了一些剪枝. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn =