CodeForces 495B Modular Equations

原创 2015年11月21日 16:55:56

Modular Equations
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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  a solution 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 Input

Input
21 5
Output
2
Input
9435152 272
Output
282
Input
10 10
Output
infinity

题意就是给出一个方程 a%x=b,其中的a和b是已知的常数,问有多少个x的解,如果解有无穷多个就输出infinity

解题思路: t = a - b;如果t%x==0 中的x是一个解,那么t/x就是另外一个解当x>b && t/x>b,如果有一个不满足>b的条件就舍去。 那么就能以sqrt(t) 为一条界限去划分1-t,那么这一条界的左边是上述满足t%x==0的x的话,右边就是t/x,这样使得时间复杂度降低到sqrt(t)。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
using namespace std;

int main()
{
	int i,j,k,t,m,n;
	
	int a,b;
	scanf("%d%d",&a,&b);
	if(a==b)
		printf("infinity\n");
	
	else if(b>a/2)
			printf("0\n");
		
	else{
		t =  a-b;
		int ans = 0;
		int tt = sqrt(t*1.0);
		for(i=1;i<=b;i++)
		{
			if(t%i==0 && t/i>b)
				ans++;
		}
		for(i=b+1;i<=tt;i++){
			if(t%i==0 )
				ans+=2;
		}
		if((tt*1.0 == sqrt(t*1.0)) && tt>b)
			ans--;
		printf("%d\n",ans);
	}
	return 0;
}






相关文章推荐

Codeforces 495 B. Modular Equations && Codeforces Round #282 (Div. 2)

题目链接:http://codeforces.com/problemset/problem/495/B 解题思路:Codeforces官方题解: If a b then there is n...

Codeforces Round #282 (Div. 2) B. Modular Equations

原题链接: http://codeforces.com/group/MlEaNIrGCZ/contest/495/problem/B B. Modular Equations ...

Codeforces Round #282 (Div. 2) B. Modular Equations&C. Treasure

B. Modular Equations 题意:给一对a、b (0

B. Modular Equations

Last week, Hamed learned about a new type of equations in his math class called Modular Equations. L...

codeforces - 495A Digital Counter

codeforces - 495A   Digital Counter time limit per test 1 second memory limit per test 256 megab...

Codeforces 495C Treasure【贪心】

C. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input stan...

codeforces 303C Minimum Modular(下标的艺术+math)

http://codeforces.com/problemset/problem/303/C 大致题意:给定一串数字,在最多移除k个数字的前提下,求出最小的数字m,使得不等式 恒成立。 分析:最简单...

Codeforces Round #131 (Div. 2) / 214A System of Equations(枚举&优化)

A. System of Equations time limit per test 2 seconds memory limit per test 256 megabytes input...

Codeforces Round #334 (Div. 2) D. Modular Arithmetic(置换)

题意:给出方程 f(kx%p)=kf(x)%p ,问在集合A->B上不同的映射函数f有几种,其中A=B={0,1,2..p-1},p为素数(除了2),k为小于p的一个常数。 思路:f(kx1%p) ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces 495B Modular Equations
举报原因:
原因补充:

(最多只允许输入30个字)