HDU 1017确定某式结果是整数

来源:互联网 发布:mac windows 10 编辑:程序博客网 时间:2024/06/06 01:47

A Mathematical Curiosity

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

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
20 3
30 4
0 0

Sample Output
Case 1: 2
Case 2: 4
Case 3: 5


思路

1、首先要读懂题目的意思(英语和语文的重要性呀!!)

2、题目的意思就是:先输入N,N控制输出块的个数。再输入n,m,然后找出符合0 < a < b < n and (a^2+b^2 +m)/(ab) is an integer.的a、b值

3、确定(a^2+b^2 +m)/(ab)是整数的方法有两个:

1: s=float(a*a + b*b + m)/float(a*b);
if(s==(int)s)
count++;
2: if((a*a+b*b+m)%(a*b)==0)
count++;

4、刚开始一直以为要等第一组数入块结束(0,0)再输出输出块,结果是我理解错了,按我这个方法一定要用数组,我就用了数组,结果AC了,后来发现,如果我的数组大小是10000+的话是可通过,但如果数组大小只定义1000左右的话就WA或Runtime Error ( ACCESS _VIOLATION)了,只想说杭电的数据没那么全!!


用数组AC的代码

#include <iostream>#include <stdio.h>using namespace std;int main(){    int N;    cin>>N;    while(N--)    {        int a=0;        int b=0;        int a1[10000]={0};        int b1[10000]={0};        int i=0;        int p=0;        int count=0;        int lo=1;        for(i=0;;i++)        {            cin>>a1[i]>>b1[i];            p++;            if(a1[i]==0&&b1[i]==0)                break;        }        for(int i=0;i<p-1;i++)        {            count=0;            for(int a=1;a<a1[i];a++)            {                for(int b=a+1;b<a1[i];b++)                {                    if((a*a+b*b+b1[i])%(a*b)==0)                        count++;                }            }            printf("Case %d: %d\n",lo++,count);        }        if(N)        printf("\n");    }    return 0;}

不用数组AC的代码(包括两个确定整数的方法)

#include <iostream>using namespace std;int main(){    int N;    cin >> N;    while(N--)    {        int cas = 1;        int n,m;        int a,b;        while(cin>>n>>m)        {            if(n==0&&m==0)                break;            int count = 0;            for(a=1; a<n; a++)            {                for(b=a+1; b<n; b++)                {                    float s;                    s=float(a*a + b*b + m)/float(a*b);//方法一                    if(s==(int)s)                        count++;                    //if((a*a+b*b+m)%(a*b)==0)  //方法二                      // count++;                }            }            cout <<"Case "<<cas++<<": "<<count<<endl;        }        if(N)            cout << endl;    }    return 0;}