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
10 5
10 7
1337 23
123454321 42
999999937 142857133
IMPOSSIBLE
3
872
14696943
166666655

题解:就是求一个不定方程 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的更多相关文章

  1. AGC027 A - Candy Distribution Again

    目录 题目链接 题解 代码 题目链接 AGC027 A - Candy Distribution Again 题解 贪心即可 代码 #include<cstdio> #include< ...

  2. HDU 5291 Candy Distribution DP 差分 前缀和优化

    Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...

  3. HDU 5291 Candy Distribution

    Candy Distribution Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. poj3372 Candy Distribution

    可以证明: f(k) = k *(k - 1)/ 2 (1 ≤ k ≤ n)是n的完全剩余系当且仅当n = 2 ^ t. http://poj.org/problem?id=3372

  5. [AtCoder AGC27A]Candy Distribution Again

    题目大意:把$x$个糖果分给$n$个人,必须分完,如果第$i$个人拿到$a_i$个糖果,就会开心,输出最多多少人开心 题解:从小到大排序,判断是否可以让他开心,注意最后判断是否要少一个人(没分完) 卡 ...

  6. [AGC027A]Candy Distribution Again

    Description AGC027A 你有一些糖果,你要把这些糖果一个不剩分给一些熊孩子,但是这帮熊孩子只要特定数目的糖果,否则就会不开心,求最多的开心人数. Solution 如果\(\sum a ...

  7. Candy 解答

    Question There are N children standing in a line. Each child is assigned a rating value. You are giv ...

  8. HDU 5291(Candy Distribution-差值dp)

    Candy Distribution Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  9. acm数学(转)

    这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...

随机推荐

  1. 使用Promethus+Grafana监控Mongodb

    note:本人周五开始接触promethuse,周一上班就开始搭环境和配置监控,其实本人对这三个东西根本就是略知一二,但是,我预测感觉过程还是简单的,结果... 首先把过程和遇到的问题一一说明. 1. ...

  2. 第二章 Getting started with functional programming

    Getting started with functional programming 开始函数式编程 higher-order functions-高阶函数 所有FP语言的主要特点是函数可以像普通值 ...

  3. windows使用的奇淫技巧

    找回刚才打开的网页 记事本 特殊符号输入 窗口变化为1/2,1/4 批量重命名 复制文件路径 万能命令

  4. html第一个程序

    2020-04-05  每日一例第27天 1.打开记事本,输入html格式语言: 2.后台代码注释: <html> <head><!--标题语句--> <ti ...

  5. JavaScript实现哈希表

    JavaScript实现哈希表 一.哈希表简介 1.1.认识哈希表 哈希表通常是基于数组实现的,但是相对于数组,它存在更多优势: 哈希表可以提供非常快速的插入-删除-查找操作: 无论多少数据,插入和删 ...

  6. 对webpack和gulp的理解和区别

    webpack是前端构建工具,称为模块打包机,webpack支持模块化:构建前端开发过程中常用的文件,如:js,css,html,img等:使用简单方便,自动化构建.webpack是通过loader( ...

  7. Codeforces 631 (Div. 2) C. Dreamoon Likes Coloring 思维or构造

    https://codeforces.com/contest/1330/problem/C 给n个格子染色,有m种颜色,要求最后的所以格子被染色,并且有m种颜色. 染色要求:每种颜色有一个值li,选择 ...

  8. Javascript/Jquery实现日期前一天后一天

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. Kubernetes实战总结 - Prometheus部署

    什么是普罗米修斯? Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包 . 自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员 ...

  10. I、恋爱之子

    链接:https://ac.nowcoder.com/acm/contest/3570/I 来源:牛客网 题目描述 最近ZSC和他的女朋友NULL吵架了.因为ZSC是一个直男,他不知道该怎么办,于是他 ...