Coin

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1020    Accepted Submission(s):
507

Problem Description
Moon has many coins, but only contains two value types
which is 5 cents and 7 cents, Some day he find that he can get any value which
greater than 23 cents using some of his coins. For instance, he can get 24 cents
using two 5 cents coins and two 7 cents coins, he can get 25 cents using five 5
cents coins, he can get 26 cents using one 5 cents coins and three 7 cents coins
and so on.

Now, give you many coins which just contains two value types
just like Moon, and the two value types identified by two different prime number
i and j. Can you caculate the integer n that any value greater than n can be
created by some of the given coins.

 
Input
The first line contains an integer T, indicates the
number of test cases.
For each test case, there are two different prime i
and j separated by a single space.(2<=i<=1000000, 2<=j<=1000000)
 
Output
For each test case, output one line contains the number
n adapt the problem description.
 
Sample Input
1
5 7
 
Sample Output
23
 
Source
 
题意:给你两种素数,每种素数个数不限,求哪个数字后所有的数字都可以通过这两种素数组合构成
 
做最短路的时候无意中看到的题,很简单的数学推理,代码短顺手敲了~
 
设所求为n,那么n+a、n+b可以用a、b线性表出,而n不可。
所以 n+a=x1*a+y1*b,n+b=x2*a+y2*b
所以 n=(x1-1)*a+y1*b n=x2*a+(y2-1)*b
因为n不能被线性表出,所以x1=0,y2=0
所以 n+a=y1*b,n+b=x2*a
所以 n+a=y1*b,n+a=(x2+1)*a-b
所以 (x2+1)*a-b是b的倍数
因为a、b互质,所以(x2+1)是b的倍数
因为求最小的n,所以选最小的x2值,所以取(x2+1)为b
所以 n+a=b*a-b,n=a*b-a-b
证毕
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#define ll __int64
using namespace std;
int main()
{
int T,i,j;
ll n,m;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&n,&m);
ll s=n*m-n-m;
printf("%I64d\n",s);
}
return ;
}

最新文章

  1. Rails--n+1查询
  2. Webpack 入门指南 - 2.模块
  3. 基于SSL协议的双向认证 - 数字证书 [2]
  4. zsh 通信脚本
  5. iOS - UIView
  6. hdu 4474 Yet Another Multiple Problem
  7. CSS基础(02)
  8. HDU1495(bfs)
  9. Linux I2C设备驱动编写(三)-实例分析AM3359
  10. jquery表单实时验证
  11. Android系统Surface机制的SurfaceFlinger服务的线程模型分析
  12. UINavigationController &#160;和 UITabBarController
  13. sql server 视图 的一个例子
  14. 线段树初步__ZERO__.
  15. http 400错误【原】
  16. struts1.x 核心控制器 和 用户自定义控制器扩展类;
  17. 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
  18. JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
  19. mysql 限制sql执行时间
  20. 前端图片压缩上传(纯js的质量压缩,非长宽压缩)

热门文章

  1. [jnhs]教训之jsp页面无法用jstl取值的坑.真他妈的奇葩,实体类的属性名不能用大写
  2. oracle习题-emp表查询练习
  3. NOIP模拟赛 17.10.10
  4. Django项目:CRM(客户关系管理系统)--11--04PerfectCRM实现King_admin注册功能03
  5. wamp httpd-vhosts.conf
  6. Poj 2104区间第k大(归并树)
  7. ecshop二次开发之视频上传
  8. Python操作SQLite数据库的方法详解
  9. GitHub上的SliddingMenu滑动过程中卡顿问题的解决的方法
  10. mybatis-plus思维导图,让mybatis-plus不再难懂