CSU 1344: Special Judge

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/79964822

题目:

Description

Given a positive integer n, find two non-negative integers ab such that a2 + bn.

Input

The first line contains the number of test cases T (1 <= T <= 1000).
For each test case, there is only one line with an integer n (1 <= n <= 109) as defined above.

Output

For each test case, output two integers ab separated by a single space such that a2 + b2 = n. If there are multiple solutions, anyone will be accepted. If there is no solution, output “-1” (without quotation marks) instead.

Sample Input

4
2
3
4
4

Sample Output

1 1
-1
0 2
2 0

Hint

还记得我们在“A Sample Problem”中说到的OJ的判题形式吗?这个题主要让我们来熟悉一下OJ的另一种名为Special Judge的判题形式。所谓的Special Judge是指OJ将使用一个特定的程序来判断我们提交的程序的输出是不是正确的,而不是单纯地看我们提交的程序的输出是否和标准输出一模一样。

一般使用Special Judge都是因为题目的答案不唯一,更具体一点说的话一般是两种情况:

(1)题目最终要求我们输出一个解决方案,而且这个解决方案可能不唯一。比如这个题目就是这样的,只要求我们输出一组a、b满足a^2 + b^2 = n,而对于不同的n,可能有多组a、b满足这一条件,像n=4时,就有a=2、b=0和a=0、b=2这两组解,这时我们输出任意一组解都可以。

(2)题目最终要求我们输出一个浮点数,而且会告诉我们只要我们的答案和标准答案相差不超过某个较小的数就可以,比如0.000001。这种情况我们只要保证我们的中间运算过程尽可能精确就可以了,并且最后输出答案的时候保留的小数位数足够多就行了,比如如果要求我们的答案和标准答案相差不超过0.01,那么一般我们保留3位小数、4位小数等等都是可以的,而且多保留几位小数也没什么坏处。

为了能让大家更清楚的了解Special Judge的原理,这里我把这个题目的执行Special Judge功能的程序的源代码贴出来了。




大家也许发现了,这个执行Special Judge功能的程序并不那么完美,比如我们在输出a和b的时候中间如过用两个空格分隔a和b的话也是可以通过这个题目的,但是这样实际上是不符合题目在Output中约定的输出格式的。我在这里之所以用了这样一个不是很完美的Special Judge,主要原因是写一个十分完美的Special Judge是要花费很大“力气”的,而且有些时候输出的格式并不不是这个题目的核心,更重要的应该是我们输出的a和b应当是符合要求的。不过大家千万不能凡是遇到Special Judge的题目就不管输出格式了,因为并不是所有写Special Judge程序的人都会像我这样偷工减料,我们最好还是要严格按照题目的要求来输出答案。

接下来我们分析一下这个题目应当怎么解决。

最容易想到的办法就是枚举所有可能的a、b,看是否存在一组a、b满足a^2 + b^2 = n。那么所有的可能的a、b有多少呢?最起码的一个限制条件就是a<=n且b<=n,不过我们还能让限制条件更“精确”一些,就是a<=sqrt(n)且b<=sqrt(n),虽然这样枚举量减少了不少,但是我们枚举所有可能的a、b的话还是要枚举sqrt(n)*sqrt(n) = n次,由于n最大可能是10^9,这样的枚举量还是不能承受。

但其实我们是没有必要枚举b的,因为当a确定时b=sqrt(n - a^2),我们可以将b直接求出来,如果b是整数的话,我们就算找到了一组解。这样如果我们枚举完所有可能的a后还没找到一组解的话,就可以说明是无解了。

如果觉得代码写起来会有困难的话,可以参考一下我在下面给出的示例代码,不过最后一定要按自己的思路写一个完整的代码(编写自己的代码时就不要再参考示例代码了,要一气呵成~)并获得“Accept”哟!O(∩_∩)O~

这个程序在计算b时实际上是在计算满足b^2 <= n - a^2的最大的b,之后再判断是否满足a^2 + b^2 = n就可以了。计算b的代码是b=(int)sqrt(n - a^2 + 0.5),之所以+0.5,一个原因是因为这样不会改变计算结果(也就是说这样求出来的仍是满足b^2 <= n - a^2的最大的b),另一个原因是怕出现浮点数误差(也就是说假如真的存在整数b满足a^2 + b^2 = n,通过(int)sqrt(n - a^2)未必能计算出正确的b,因为毕竟sqrt()这个函数是浮点数运算,万一算出的b丢失了一些精度那么向下取整之后就会变成b-1了)。


代码:

#include <iostream>
#include<math.h>
using namespace std;

int main()
{
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		int i = 0, j = -1;
		bool b = true;
		for (; i*i <= n; i++)
		{
			j = (int)(sqrt(n - i*i+0.1));
			if (j*j == n - i*i)
			{
				b = false;
				break;
			}
		}
		if (!b)cout << i << " " << j;
		else cout << -1;
		cout << endl;
	}
	return 0;
}

Special Judge

01-18

DescriptionnnJohn's college will hold a programming contest, and now it's calling for problems. John wants to contribute a problem to this contest. After finishing the problem, he found some trouble in verifying the programmer's output. For most programming problems, you can just compare the programmer's output against the correct answer, and if they are literally the same, the programmer's output is correct, otherwise it's wrong. But for this problem, there may be many correct answers and they can be different from each other literally. So John must write a program to verify programmers' output (We can call this program "Special Judge"). Soon, he found the Special Judge is too difficult for him, so he turns to you for help. nnHere is the description of John’s original problem: nOriginal Problem n/* Adapted from "Help the problem setter", Ulm Local 2005 */ nnLet us define a binary search tree inductively as follows: nThe empty tree which has no node is a binary search tree; nEach non-empty binary search tree has a root, which is a node labeled with an integer, and two binary search trees as left and right subtrees of the root; nThe labels of the nodes in the left subtree are all less than the label of the root; nThe labels of the nodes in the right subtree are all greater than the label of the root.nnGiven such a binary search tree, the following search procedure can be used to locate a node in the tree: Start with the root node. Compare the label of the current node with the desired label. If it is the same, you have found the right node. Otherwise, if the desired label is smaller, search in the left subtree, otherwise search in the right subtree. nnThe access frequency of a node is the frequency you search for the node. The access cost to locate a node is the number of nodes you have to visit until you find the right node. Given some nodes labeled with integers and the access frequency of every node, an optimal binary search tree is a binary search tree consisting of these nodes with the minimum expected access cost. nnHere is your task: Given a binary search tree, try to specify the access frequency of every node, for which this binary search tree is the unique optimal binary search tree (The unique optimal binary search tree has the minimum expected access cost, which is less than any other binary search tree consisting of the same nodes with the same access frequencies). nOriginal Problem's Input nThe input contains one test case. The test case starts with an integer N (1 <= N <= 50), the number of nodes in the optimal binary search tree. For simplicity, the labels of the nodes will be integers from 1 to N. The following N lines describe the structure of the tree. The i-th line contains the labels of the roots of the left and right subtrees of the node with label i (or -1 for an empty subtree). You can assume that the input always defines a valid binary search tree. nOriginal Problem's Output nFor the test case, write one line containing the access frequency for each node in increasing order of the labels of the nodes. To avoid problems with floating point precision, the frequencies should be written as non-negative integers without leading zeros (meaning the access probability for a node will be the frequency divided by the sum of all frequencies). Make sure that you do not write any integer bigger than 1015. nOriginal Problem's Sample Input n3 n-1 -1 n1 3 n-1 -1 nOriginal Problem's Sample Output n1 1 1 nOriginal Problem's Hint nNote that the test case in the sample input describes a tree looking like: n 2 nn / \ nn1 3nnnYou will be given the input of the original problem and the output of a programmer, your task is to determine whether the programmer's output is correct or not.nInputnnThe input contains several test cases, and there are two sections in each test case. The first section is the original problem's input started by "#Start Input#" and ended by "#End Input#". The second section is the programmer's output started by "#Start Programmer's Output#" and ended by "#End Programmer's Output#". A line with "#End#" indicates the end of the input. nnYou can assume that all the data in the first section is valid, but the second section may contain anything except the string "#End Programmer's Output#". The valid programmer's output always contains 1 line, which means if you find that the programmer's output is less or more than 1 line, it's obviously wrong. Each line in the programmer's output will not exceed 2000 characters.nOutputnnFor each test case output "Accepted" if the programmer's output is correct corresponding to the original problem, or "Wrong" if it's wrong. You should ignore the extra spaces or tabs ('\t') in the programmer's output and you can only ignore these 2 characters.nSample Inputnn#Start Input#n3n-1 -1n1 3n-1 -1n#End Input#n#Start Programmer's Output#n1 1 1n#End Programmer's Output#n#Start Input#n10n-1 2n-1 3n-1 4n-1 5n-1 6n-1 7n-1 8n-1 9n-1 10n-1 -1n#End Input#n#Start Programmer's Output#n512 256 128 64 32 16 8 4 2 89798n#End Programmer's Output#n#End#nSample OutputnnAcceptednWrong

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试