Codeforces Round #282 (Div. 2) --- B. Modular Equations,
Modular Equations
Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Last week, Hamed learned about a new type of equations in his math class called Modular Equations. Lets defineIModuloJAs the remainder of divisionIByJAnd denote it by. A Modular Equation, as Hamed's teacher described, is an equation of the form in whichAAndBAre two non-negative integers andXIs a variable. We call a positive integerXFor 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 numbersAAndBDetermines how many answers the Modular Equation has.
Input
In the only line of the input two space-separated integersAAndB(0 bytes ≤ bytesA, Bytes,BLimit ≤ limit 109) are given.
Output
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.
Sample test (s) input
21 5
Output
2
Input
9435152 272
Output
282
Input
10 10
Output
infinity
Note
In the first sample the answers of the Modular Equation are 8 and 16 since
Question: Give a, B, and ask how many positive integers x meet a % x = B exist.
Analysis: brute force attacks can be solved. A % x = B has (a-B) % x = 0, that is, finding the factor of a-B. The premise is that x is a positive integer, but note that x> B must be satisfied (the remainder is smaller than the divisor). When a <B, if x does not meet the condition, output 0; when a = B, the "infinity" should be output; otherwise, you can directly find the factor of a-B.
AC code:
#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 version:
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