hdu A Mathematical Curiosity

来源:互联网 发布:燃气灶烤箱一体机 知乎 编辑:程序博客网 时间:2024/06/01 08:50

A Mathematical Curiosity

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 87   Accepted Submission(s) : 23

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a^2+b^2 +m)/(ab) is an integer.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

You will be given a number of cases in the input. Each case is specified by a line containing the integers n and m. The end of input is indicated by a case in which n = m = 0. You may assume that 0 < n <= 100.

Output

For each case, print the case number as well as the number of pairs (a,b) satisfying the given property. Print the output for each case on one line in the format as shown below.

Sample Input

110 120 330 40 0

Sample Output

Case 1: 2Case 2: 4Case 3: 5
题意:给出两个数n和m让你找出满足条件的情况数:条件是a,b满足0<a<b<n,并且(a^2+b^2 +m)/(ab)是个整数。
解题思路:直接枚举,对于(a^2+b^2 +m)/(ab)是整数的判断有多种,1:(a^2+b^2 +m)%(ab)等于零。2:用double型求出结果并记录,使之转化成整型判断前后是否相等。
注意输出格式:第一行就是要测试数据的组数(就是那么多的数据作为一组)。 然后下边的数据就是每一小组要测试的数据 并且每一小组以0 0结束,每一行结果要换行,每一组要换行,其中最后一大组结束时不换行。
3
10 1
2 5
3 3
6 8
0 0
 
2 9
3 7
0 0
 
6 6
5 4 
9 8
0 0
这就表示有三大组,每组的测试数据个数不定,以 0 0 结束。
代码:

#include <iostream>using namespace std;int main(){ int t,s,n,m,a,b,p; cin>>t; while(t--) {  int k=1;  while(cin>>n>>m&&(n!=0||m!=0))  {   p=0;      for(a=1;a<n-1;a++)    for(b=a+1;b<n;b++)    {

/*法2:     double sum=double(a*a+b*b+m)/(a*b);     s=int(sum);     if(s==sum)*/

//法1:

s=(a*a+b*b+m)%(a*b)

if(s==0)      p++;    }    cout<<"Case "<<k++<<": "<<p<<endl;  }

  if(t)   cout<<endl; } return 0;}

 

0 0
原创粉丝点击