Candy Distribution
Kids like candies, so much that they start beating each other if the candies are not fairly distributed. So on your next party, you better start thinking before you buy the candies.
If there are KK kids, we of course need K⋅XK⋅X candies for a fair distribution, where XX is a positive natural number. But we learned that always at least one kid looses one candy, so better be prepared with exactly one spare candy, resulting in (K⋅X)+1(K⋅X)+1 candies.
Usually, the candies are packed into bags with a fixed number of candies CC. We will buy some of these bags so that the above constraints are fulfilled.
Input
The first line gives the number of test cases tt (0<t<1000<t<100). Each test case is specified by two integers KK and CC on a single line, where KK is the number of kids and CC the number of candies in one bag (1≤K,C≤1091≤K,C≤109). As you money is limited, you will never buy more than 109109 candy bags.
Output
For each test case, print one line. If there is no such number of candy bugs to fulfill the above constraints, print “IMPOSSIBLE” instead. Otherwise print the number of candy bags, you want to buy. If there is more than one solution, any will do.
| Sample Input 1 | Sample Output 1 |
|---|---|
5 |
IMPOSSIBLE |
题解:就是求一个不定方程 k*x-c*y=1 有几个注意的地方 对c==1的情况进行特判 不能有puts输出 还有就是不能有(x%d+d)%d来更新x
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==){
x=;
y=;
return a;
}
ll r=exgcd(b,a%b,x,y);
ll t=y;
y=x-(a/b)*y;
x=t;
return r;
}
int main(){
ios::sync_with_stdio(false);
ll t;
cin>>t;
while(t--){
ll k,c;
cin>>k>>c;
ll x,y;
ll r=exgcd(c,k,x,y);
if(r!=){
// puts("IMPOSSIBLE");
cout<<"IMPOSSIBLE"<<'\n';
}
else {
if(c==) {
cout<<k+<<'\n';
}
else{
while(x<=) x+=k;
cout<<x<<'\n';
}
}
}
return ;
}
Candy Distribution的更多相关文章
- AGC027 A - Candy Distribution Again
目录 题目链接 题解 代码 题目链接 AGC027 A - Candy Distribution Again 题解 贪心即可 代码 #include<cstdio> #include< ...
- HDU 5291 Candy Distribution DP 差分 前缀和优化
Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...
- HDU 5291 Candy Distribution
Candy Distribution Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- poj3372 Candy Distribution
可以证明: f(k) = k *(k - 1)/ 2 (1 ≤ k ≤ n)是n的完全剩余系当且仅当n = 2 ^ t. http://poj.org/problem?id=3372
- [AtCoder AGC27A]Candy Distribution Again
题目大意:把$x$个糖果分给$n$个人,必须分完,如果第$i$个人拿到$a_i$个糖果,就会开心,输出最多多少人开心 题解:从小到大排序,判断是否可以让他开心,注意最后判断是否要少一个人(没分完) 卡 ...
- [AGC027A]Candy Distribution Again
Description AGC027A 你有一些糖果,你要把这些糖果一个不剩分给一些熊孩子,但是这帮熊孩子只要特定数目的糖果,否则就会不开心,求最多的开心人数. Solution 如果\(\sum a ...
- Candy 解答
Question There are N children standing in a line. Each child is assigned a rating value. You are giv ...
- HDU 5291(Candy Distribution-差值dp)
Candy Distribution Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
随机推荐
- 数学-绝对值-Reverse Subarray To Maximize Array Value
2020-02-11 12:01:21 问题描述: 问题求解: 本题的难度个人感觉还是蛮大的,主要是不容易想到O(n)的解. 对于 ...a, [b, ... , c], d, ...,如果我们将其中 ...
- Check If It Is a Straight Line
2019-10-21 10:35:33 问题描述: 问题求解: public boolean checkStraightLine(int[][] coordinates) { int n = coor ...
- FormDataBodyPart获取表单文件名乱码解决方法
FormDataMultiPart formData=; FormDataBodyPart filePart=; filePart.getFormDataContentDisposition().ge ...
- 使用skimage处理图像数据的9个技巧
介绍 我们非常熟悉结构化(表格)数据的预处理步骤.你可以找到缺失的值然后添补它,然后检测并处理异常值,等等这些步骤.这有助于我们建立更好.更健壮的机器学习模型.但是当我们处理图像数据时,应该如何进行预 ...
- TensorFlow系列专题(十一):RNN的应用及注意力模型
磐创智能-专注机器学习深度学习的教程网站 http://panchuang.net/ 磐创AI-智能客服,聊天机器人,推荐系统 http://panchuangai.com/ 目录: 循环神经网络的应 ...
- PHP7内核(八):深入理解字符串的实现
在前面大致预览了常用变量的结构之后,我们今天来仔细的剖析一下字符串的具体实现. 一.字符串的结构 struct _zend_string { zend_refcounted_h gc; /* 字符串类 ...
- 使用SpringCloud将单体迁移至微服务
使用SpringBoot构建单体项目有一段时间了,准备对一个老项目重构时引入SpringCloud微服务,以此奠定后台服务能够应对未知的业务需求. 现在SOA架构下的服务管理面临很多挑战,比如面临一个 ...
- RuntimeError: PyTorch was compiled without NumPy support
原因:Pytorch和Numpy版本不匹配 查看自己Pytorch和Numpy版本 (1)执行[pip show torch]和[pip show numpy]查看版本信息(可通过[pip -h]查看 ...
- 瀑布流vue-waterfall的高度设置
最近用vue做项目,用到了瀑布流vue-waterfall,其中遇到高度的设置问题,大概介绍下,希望可以帮到一些人 1.安装 npm install --save vue-waterfall 2.引入 ...
- Linux下使用FastDFS
本文所有操作均在CentOS 7.x环境下进行. 1.1.单节点FastDFS 整个安装过程非常复杂,很容易出错,建议进行多次备份. 我们这里不打算安装多台虚拟机,因此会把tracker和storag ...