Codeforces Round #282 (Div. 2)---B. Modular Equations
来源:互联网 发布:参考文献来源于网络 编辑:程序博客网 时间:2024/05/18 23:29
Last week, Hamed learned about a new type of equations in his math class called Modular Equations. Lets define i modulo j as the remainder of division of i by j and denote it by . A Modular Equation, as Hamed's teacher described, is an equation of the form in which a and b are two non-negative integers and x is a variable. We call a positive integer x for which asolution of our equation.
Hamed didn't pay much attention to the class since he was watching a movie. He only managed to understand the definitions of these equations.
Now he wants to write his math exercises but since he has no idea how to do that, he asked you for help. He has told you all he knows about Modular Equations and asked you to write a program which given two numbers a and b determines how many answers the Modular Equation has.
In the only line of the input two space-separated integers a and b (0 ≤ a, b ≤ 109) are given.
If there is an infinite number of answers to our equation, print "infinity" (without the quotes). Otherwise print the number of solutions of the Modular Equation .
21 5
2
9435152 272
282
10 10
infinity
In the first sample the answers of the Modular Equation are 8 and 16 since
题意:给出a,b,问有多少满足a % x == b的正整数x存在。
分析:暴力可解。a % x == b有(a - b) % x == 0,也就是找a - b的因子。前提是:x是正整数,但是要注意需满足x > b(余数比除数小),当a < b时,此时没有x满足条件,输出0即可;当a == b时,应输出“infinity”;否则的话,直接暴力找a - b的因子即可。
AC代码:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7fffffffint main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int a, b, ans; while(scanf("%d%d",&a, &b)!=EOF) { ans = 0; if(a < b) puts("0"); else if(a == b) puts("infinity"); else{ int x; for(x=1; x*x<a-b; x++){ if((a-b) % x == 0){ if(x > b) ans ++; if((a-b)/x > b) ans ++; } } if((a-b) == x*x && x > b) ans ++; printf("%d\n", ans); } } return 0;}
Python版:
a, b = map(int, raw_input().split())if a == b: print 'infinity'elif a < b: print 0else: a -= b i = 1 ans = 0 while i*i <= a: if a % i == 0: if i > b: ans += 1 if a/i > b and i*i != a: ans += 1 i += 1 print ans
- Codeforces 495 B. Modular Equations && Codeforces Round #282 (Div. 2)
- Codeforces Round #282 (Div. 2)---B. Modular Equations
- Codeforces Round #282 (Div. 2) B. Modular Equations&C. Treasure
- Codeforces Round #282 (Div. 2) B. Modular Equations
- Codeforces Round #282 (Div. 2) A - Digital Counter B - Modular Equations C - Treasure
- CodeForces 495B Modular Equations
- codeforces 495B - Modular Equations
- CF 282B Modular Equations
- 495B - Modular Equations
- B. Modular Equations
- Codeforces Round #334 (Div. 2) D. Modular Arithmetic(置换)
- Codeforces Round #282 (Div. 2) A B
- Codeforces Round #282 (Div. 2) B.(数学)
- Codeforces Round #282 (Div. 1) B
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #134 (Div. 2)B. Airport
- Codeforces Round #170 (Div. 2) problem B
- Codeforces Round #173 (Div. 2) Problem B
- 宽带叠加,网速累积的方法 合并多个无线网络来提高网速
- 关于java输入初涉
- 【屌丝程序的口才逆袭演讲稿50篇】第一篇:互联网时代U盘化生存方式 【张振华.Jack】
- Codeforces Round #282 (Div. 2)---A. Digital Counter
- hdu3078
- Codeforces Round #282 (Div. 2)---B. Modular Equations
- how do I delete an Eclipse project when I've already deleted the folder and contents?
- 应届生高薪就业的背后
- 爱情四阶段
- 设计高效sql一般经验谈
- 数据结构之排序
- 求灰度图像最大灰度,最小灰度,平均灰度
- 高效C++ --经验条款(一)
- 设置CATALINA_HOME环境变量