首页 > ACM题库 > HDU-杭电 > HDU 1017 A Mathematical Curiosity[解题报告] C++
2013
11-26

HDU 1017 A Mathematical Curiosity[解题报告] C++

A Mathematical Curiosity

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

1

10 1
20 3
30 4
0 0

样例输出:

Case 1: 2
Case 2: 4
Case 3: 5

题目大意:

  给定一个N,表示有N组测试数据。每组测试数据中每次给定n 和 m。当n = m = 0时结束本组测试数据。得到n 和m 之后,需要计算令(a ^ 2 + b ^ 2 + m) / (ab) 为整数的a, b的组数。。a, b满足(0 < a < b < n)。

===================================

  这道题有点小猥琐。。各种PE。。需要注意格式问题,每个BLOCK之间都要有空行,最后一个BLOCK没有。此外,我居然把<n 写成了 <=n 。。有种想死的感觉。。

 

===================================

#include "stdio.h"
int deal(int n, int m)
{
int a, b;
long long r;
int res = 0;
for (a = 1; a < n; a++)
{
       for (b = a + 1; b < n; b++)
              {
                     r = (a * a + b * b + m) % (a * b);
                     if (!r)
                            res += 1;
              }
       }
       return res;
}

int main()
{
       int N;
       bool mark = 0;
       scanf ("%d", &N);
       int n, m;
       while (N--)
       {
              int i = 1;
              (mark) ? printf ("\n") : mark = 1;
              while (scanf ("%d %d", &n, &m) != EOF && (n || m))
                     printf ("Case %d: %d\n", i++, deal(n, m));
       }
       return 0;
}

  1. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。

  2. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧

  3. 可以参考算法导论中的时间戳。就是结束访问时间,最后结束的顶点肯定是入度为0的顶点,因为DFS要回溯