CF991A If at first you don't succeed...

本文解析了CodeForces平台上的一道A级题目,通过容斥原理解决学生考试通过情况的问题。介绍了输入输出要求及样例说明。

原题链接:http://codeforces.com/contest/991/problem/A

If at first you don’t succeed…

Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn’t pass it. However, many of Vasya’s fellow students from the same group were more successful and celebrated after the exam.

Some of them celebrated in the BugDonalds restaurant, some of them — in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn’t pass the exam didn’t celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by A A students, BeaverKing — by B students and C C students visited both restaurants. Vasya also knows that there are N students in his group.

Based on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn’t, how many students in his group didn’t pass the exam. Can you help him so he won’t waste his valuable preparation time?

Input

The first line contains four integers — A,B,C A , B , C and N N (0A,B,C,N100).

Output

If a distribution of N N students exists in which A students visited BugDonalds,
B B — BeaverKing, C — both of the restaurants and at least one student is left home (it is known that Vasya didn’t pass the exam and stayed at home), output one integer — amount of students (including Vasya) who did not pass the exam.

If such a distribution does not exist and Vasya made a mistake while determining the numbers A A , B, C C or N (as in samples 2 and 3), output −1.

Examples
input

10 10 5 20

output

5

input

2 2 0 4

output

-1

input

2 2 2 1

output

-1

Note

The first sample describes following situation: 5 5 only visited BugDonalds, 5 students only visited BeaverKing, 5 5 visited both of them and 5 students (including Vasya) didn’t pass the exam.

In the second sample 2 2 students only visited BugDonalds and 2 only visited BeaverKing, but that means all 4 4 students in group passed the exam which contradicts the fact that Vasya didn’t pass meaning that this situation is impossible.

The third sample describes a situation where 2 students visited BugDonalds but the group has only 1 1 which makes it clearly impossible.

题解

基本容斥原理。

代码
#include<bits/stdc++.h>
using namespace std;
int a,b,c,n,ans;
void in(){scanf("%d%d%d%d",&a,&b,&c,&n);}
void ac()
{
    ans=n-(a+b-c);
    if(a>n||b>n||c>n||c>a||c>b||ans>n||ans<1)puts("-1"),exit(0);
    printf("%d",ans);
}
int main(){in();ac();}
在Ubuntu系统中,“At least one valid and enabled repository required for this action to succeed” 错误通常表示系统在执行某些操作(如软件安装、更新等)时,没有找到有效的软件源仓库。以下是一些可能的解决方法: ### 检查并更新软件源列表 Ubuntu的软件源列表文件通常位于 `/etc/apt/sources.list`,可以使用文本编辑器(如 `nano` 或 `vim`)打开该文件进行检查和编辑。 ```bash sudo nano /etc/apt/sources.list ``` 确保文件中包含有效的软件源地址。例如,对于Ubuntu 20.04,可以使用以下官方软件源: ```plaintext deb http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse deb http://security.ubuntu.com/ubuntu focal-security main restricted universe multiverse ``` 编辑完成后,按 `Ctrl + X`,然后按 `Y` 保存更改,最后按 `Enter` 退出。 ### 更新软件源缓存 在修改软件源列表后,需要更新软件源缓存,使更改生效。 ```bash sudo apt update ``` ### 启用官方软件源 如果软件源列表中没有启用某些官方软件源,可以通过以下命令启用: ```bash sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main restricted universe multiverse" sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-updates main restricted universe multiverse" sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-backports main restricted universe multiverse" sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu $(lsb_release -sc)-security main restricted universe multiverse" ``` 添加完成后,再次更新软件源缓存: ```bash sudo apt update ``` ### 检查网络连接 确保系统能够正常访问软件源服务器。可以通过 `ping` 命令测试网络连接: ```bash ping archive.ubuntu.com ``` 如果无法ping通,可能是网络配置问题,需要检查网络设置。 ### 检查代理设置 如果系统使用了代理,可能会影响软件源的访问。可以通过以下命令查看和修改代理设置: ```bash sudo nano /etc/apt/apt.conf.d/99proxy ``` 如果文件中存在代理设置,可以将其注释掉或修改为正确的代理地址。 ### 检查防火墙设置 防火墙可能会阻止系统访问软件源服务器。可以通过以下命令检查和修改防火墙设置: ```bash sudo ufw status ``` 如果防火墙处于开启状态,可以允许HTTP和HTTPS流量: ```bash sudo ufw allow http sudo ufw allow https ``` ### 重新安装 `apt` 相关包 如果以上方法都无法解决问题,可以尝试重新安装 `apt` 相关包: ```bash sudo apt-get install --reinstall apt apt-utils ``` 安装完成后,再次更新软件源缓存: ```bash sudo apt update ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值