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. 解析“60k”大佬的19道C#面试题(下)

    解析"60k"大佬的19道C#面试题(下) 在上篇中,我解析了前 10 道题目,本篇我将尝试解析后面剩下的所有题目. 姐妹篇:解析"60k"大佬的19道C#面试 ...

  2. Building Applications with Force.com and VisualForce(Dev401)( 九):Designing Applications for Multiple Users: Putting It All Together

    Module Objectives1.Apply profiles, organization wide defaults, role hierarchy and sharing to given a ...

  3. ReentrantLock源码解析——虽众但写

    在看这篇文章时,笔者默认你已经看过AQS或者已经初步的了解AQS的内部过程.   先简单介绍一下ReentantLock,跟synchronized相同,是可重入的重量级锁.但是其用法则相当不同,首先 ...

  4. EF多租户实例:快速实现分库分表

    前言 来到这篇随笔,我们继续演示如何实现EF多租户. 今天主要是演示多租户下的变形,为下图所示 实施 项目结构 这次我们的示例项目进行了精简,仅有一个API项目,直接包含所有代码. 其中Control ...

  5. 常见Web安全漏洞--------防盗链

    1,防盗链防止盗用自己服务上的东西... 2,XSS服务上有这么一张图: <!DOCTYPE html> <html> <head lang="en" ...

  6. java NIO理解分析与基本使用

    我前段时间的一篇博客java网络编程--多线程数据收发并行总结了服务端与客户端之间的收发并行实践.原理很简单,就是针对单一客户端,服务端起两个线程分别负责read和write操作,然后线程保持阻塞等待 ...

  7. SpringBoot使用RedisTemplate操作Redis时,key值出现 \xac\xed\x00\x05t\x00\tb

    原因分析 原因与RedisTemplate源码中的默认序列化方式有关 defaultSerializer = new JdkSerializationRedisSerializer( classLoa ...

  8. 医学图像分割-在3DSlicer中使用英伟达的AI辅助工具NvidiaAIAssistedAnnotation自动切割医学图像教程

    前期准备 下载3DSlicer并安装: 3DSlicer官网(http://www.slicer.org)或者直接下载(https://download.slicer.org),需要注意目前该插件只支 ...

  9. Mysql数据库的基本操作(1)

    一.启动数据库 1. 我的电脑(此电脑)--->右键点击[管理]--->[服务和应用程序]--->[服务] 找到MySQL8.0可以选择手动启动或者自动启动. 2.可以直接通过命令行 ...

  10. IOS部分APP使用burpsuite抓不到包原因

    曾经在ios12的时候,iphone通过安装burpsuite的ca证书并开启授权,还可以抓到包,升级到ios13后部分app又回到以前连上代理就断网的情况. 分析:ios(13)+burpsuite ...