zoukankan      html  css  js  c++  java
  • B. Modular Equations

    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 

    这题的题意是求(n-m)比m大的因数个数,如果直接求会超时,所以可以用循环从1到i*i<=n-m;然后判断当前i和(n-m)/i是否大于m.

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	int n,m,i,j,num,t;
        while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		if(n==m){
    			printf("infinity
    ");continue;
    		}
    		num=0;
    		for(i=1;i*i<=n-m;i++){
    			if((n-m)%i==0){
    				t=(n-m)/i;
    				if(t>m)num++;
    				if(i>m && i!=t)num++;
    			}
    		}
    		printf("%d
    ",num);
    	}
    }

  • 相关阅读:
    xpath取其中几个使用position
    pycharm2018.3.5 下载激活(windows平台)
    switch host 切换本地host
    leveldb 学习记录(四)Log文件
    bittorrent 学习(一) 种子文件分析与bitmap位图
    分布式协议学习笔记(三) Raft 选举自编写代码练习
    谷歌开源的一个BTREE实现 Go语言
    分布式协议学习笔记(二) 日志复制
    分布式协议学习笔记(一) Raft 选举
    利用redis制作消息队列
  • 原文地址:https://www.cnblogs.com/herumw/p/9464842.html
Copyright © 2011-2022 走看看