原题链接:
http://codeforces.com/group/MlEaNIrGCZ/contest/495/problem/B
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有多少个解。
思路:
①a=b,无数解。
②a<b,无解。
③a>b,等式可化为(a-b)%x=0,即找a-b的因子数,直接暴力搜索,复杂度为O(根号(a-b)),可以过,还要注意,x必须满足小于b。
代码:
#include "stdio.h"
int a,b,c,ans,i;
int main()
{
scanf("%d%d",&a,&b);
if(a==b)
printf("infinity\n");
else if(a<b)
printf("0\n");
else
{
c=a-b;
for(i=1;i*i<c;i++)
{
if(c%i==0)
{
if(i>b)
ans++;
if(c/i>b)
ans++;
}
}
if(i*i==c&&i>b)
ans++;
printf("%d\n",ans);
}
return 0;
}