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的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
随机推荐
- (一)iview的校验TypeError: Cannot read property 'validateField' of undefined"
一.问题描述 我是在自己封装了一个地址级联选择,然后想要每次改变了其中数据的时候,就进行一次单独校验,所以用到了iview对部分表单字段进行校验的方法validateField.其实一开始使用的时候是 ...
- spring-cloud-gateway过滤器实践
概述 这里是 SpringCloud Gateway 实践的第一篇,主要讲过滤器的相关实现.Spring-Cloud-Gateway 是以 WebFlux 为基础的响应式架构设计, 是异步非阻塞式的, ...
- 深入理解JavaScript中的堆与栈 、浅拷贝与深拷贝
JavaScript中的浅拷贝与深拷贝 学了这么长时间的JavaScript想必大家对浅拷贝和深拷贝还不太熟悉吧,今天在项目中既然用到了,早晚也要理清一下思路了,在了解之前,我们还是先从JavaSc ...
- SG函数(斐波那契博弈) Fibonacci again and again
https://zhuanlan.zhihu.com/p/53948422 HDU - 1848 将这篇文章认真的看了一遍 ,虽然不是很懂 ,但是脑子里有了一个模型,链接里的图 (看的顺序 是 0,1 ...
- ORA-01017的一种情况:sysdba可以登录,normal不可登录
在arcCatalog中创建完sde数据库之后,用PLSQL登录提示只能用SYSDBA登录. 用户名:sde 密码:123456 数据库:ORCLZLL 连接为:Normal 点击登录 ...
- python框架-Django安装使用
1.安装pip sudo apt-get install python-pip 遇到问题需要更新下语言包 sudo apt-get update 检查pip是否安装成功 pip -V 查看已安装包 p ...
- [vijos1048]送给圣诞夜的贺卡<DFS剪枝>
题目链接:https://www.vijos.org/p/1048 很多人一看就想出了思路,不就是一个裸的dfs蛮...但是..在n<=50的情况下,朴素会直接tle..... 然后我就开始剪枝 ...
- shell大全
1.shell判断文件是否存在 http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html
- 【学习笔记】CART算法
1. 背景介绍 CART(Classification and Regression Trees,分类回归树)算法是一种树构建算法,既可以用于分类,也可以用于回归.它的工作原理是:使用二元切分来处理连 ...
- Maximum splitting
Maximum splitting You are given several queries. In the i-th query you are given a single positive i ...