ACM-1017 A Mathematical Curiosity

A Mathematical Curiosity

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49888    Accepted Submission(s): 16141


 

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

1

10 1

扫描二维码关注公众号,回复: 4099191 查看本文章

20 3

30 4

0   0

Sample Output

Case 1: 2

Case 2: 4

Case 3: 5

简单数学题,题目大意是给定两个整数N,M,求满足(a ^ 2 + b ^ 2 + m)/(ab)为整数的(a,b)的对数(0<a<b<n)。

题倒是不难,写一个二重循环满足公式计数就可以,输出表达的有点不清楚,搞得的我心态有点崩,WA了好几次。

输出的大概意思是这样,样例分为t组,每组有无限多个样例,以m=n=0为结束。每组之间用空行隔开(注意,每组之间,最后一组之后不加空行)

贴出N次WA之后AC的代码。可能有些人觉得我的代码写的太开,其实看每个人风格吧,个人觉得这样容易改错,内存当然也不会占用多少。

#include<bits/stdc++.h>
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

int main()
{
int t,m,n;
	cin>>t;
	for(int i=0;i<t;i++)
	{	
		int ca=1;    //样例数从1加
		while(cin>>n>>m&&(n||m))
		{
			int count = 0 ;
			for(int k=1;k<n;k++)
			{
				for(int j=k+1;j<n;j++)
				{
					if((j*j+k*k+m)%(k*j)==0)
					{
						count++;
					}
				}
			}
			cout<<"Case "<<ca<<": "<<count<<endl;       //注意大写!!
			ca++;
		}
		if(i<t-1)			cout<<endl; //组之间空行
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36791466/article/details/84135765