CodeForce 495B Modular Equations 【数学】

B. Modular Equations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

In the only line of the input two space-separated integers a and b (0 ≤ a, b ≤ 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 


题意:给出a, b的值, 找出满足a%x = b中x的个数。
分析:由同余定理可知如果上述方程成立,那么有 (a-b)%x == 0;故我们只需枚举满足 (a-b)%i == 0&&i> b的个数即可。
注意,有不确定的时候就是a=b.而且在我们枚举的时候有 只需要枚举到i*i 《= (a-b)即可因为 找到i*(n/i) =  n。
代码:
#include <cstdio>
#include <cmath>
#include <cstring>

int main(){
	int a, b, ans;
	while(~scanf("%d%d", &a, &b)){
		if(a == b){
			printf("infinity\n");continue;
		}
		ans = 0;
		int x = a-b, i;
		for(i = 1; i*i < x; i ++){
			if(x%i == 0){
				if(i > b) ++ans;
				if(x/i > b) ++ans;
			}
		}
		if(i*i == x&&i > b) ++ans;
		printf("%d\n", ans);
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值