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. 参加Folding@Home(FAH)项目,为战胜新冠肺炎贡献出自己的一份力量

    鉴于新冠病毒(COVID-19)在全球范围内的大规模传播,PCMR和NVIDIA呼吁全球PC用户加入Folding@home项目贡献自己闲置的GPU计算力,协助抗击新冠状病毒疫情. 目前全球有超过40 ...

  2. 02 Python函数与模块

     • 函数是什么 print() input() format() list() lower() upper()  • 函数的定义 def 函数名(形参1,2,3)  函数体 return  • 函数 ...

  3. Linux常用命令 - find命令基础使用(重点)

    1篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 首先,先 ...

  4. Fedora20在神州战神K650D1安装过程,使用netinstall和Dvd.iso镜像安装。

    最近新买一笔记本,神州战神K650D-i5 D1.仍旧安装双系统,WIndows7+Fedora20.磁盘分区是这样的: 第一主分区 /boot ext4 20G 第二主分区 / ext4 70G 第 ...

  5. SpringBoot 集成多数据源

    一个项目中怎么划分数据库,可以通过具体业务需求. 项目中数据源怎么如何划分,通过注解的方式@Datasource(ref="") 在方法上指定,会连接指定的数据源,这种方式比较繁琐 ...

  6. Windows 7系统记录

    http://www.winwin7.com/ win7带USB3.0和NVME驱动 http://www.mohuishou.com/ 其中的青苹果家园 支持UEFI+GPT机器 http://ww ...

  7. Java工程师技能点梳理

    从个人技术积累的角度,来看看一名合格的Java工程师在面试时所需要的知识技能. 1.基本语法 这包括static.final.transient等关键字的作用,foreach循环的原理等等.今天面试我 ...

  8. ATM购物车+三层结构项目设计

    ATM购物车项目 模拟实现一个ATM + 购物商城程序. 该程序实现普通用户的登录注册.提现充值还款等功能,并且支持到网上商城购物的功能. 账户余额足够支付商品价格时,扣款支付:余额不足时,无法支付, ...

  9. 【杂谈】SpringBoot为啥不用配置启动类

    前言 在学习SparkJava.Vert.x等轻量级Web框架的时候,都遇到过打包问题,这两个框架打包的时候都需要添加额外的Maven配置,并指定启动类才能得到可执行的JAR包: 而springboo ...

  10. echarts设置图标图例legend多种形状

    legend: {   icon: "circle",   //  字段控制形状  类型包括 circle,rect,line,roundRect,triangle,diamond ...