HDU6440-Dream-CCPC2018网络赛

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=6440

Problem Description
Freshmen frequently make an error in computing the power of a sum of real numbers, which usually origins from an incorrect equation (m+n)p=mp+np, where m,n,p are real numbers. Let's call it ``Beginner's Dream''.

For instance, (1+4)2=52=25, but 12+42=17≠25. Moreover, 9+16−−−−−√=25−−√=5, which does not equal 3+4=7. 

Fortunately, in some cases when p is a prime, the identity
(m+n)p=mp+np

holds true for every pair of non-negative integers m,n which are less than p, with appropriate definitions of addition and multiplication.

You are required to redefine the rules of addition and multiplication so as to make the beginner's dream realized.

Specifically, you need to create your custom addition and multiplication, so that when making calculation with your rules the equation (m+n)p=mp+np is a valid identity for all non-negative integers m,n less than p. Power is defined as
ap={1,ap−1⋅a,p=0p>0


Obviously there exists an extremely simple solution that makes all operation just produce zero. So an extra constraint should be satisfied that there exists an integer q(0

题目描述超长,所以我们提取关键信息。

在output里面说了详细输出规则

The j-th(1≤j≤p) integer of i-th(1≤i≤p) line denotes the value of (i−1)+(j−1).

The j-th(1≤j≤p) integer of (p+i)-th(1≤i≤p) line denotes the value of (i−1)⋅(j−1).

所以...我们直接写两个双层for循环,打印内容即可。

但有注意的几点:

1.在判断换行和空格的时候,不要写在printf后面,应该写在printf里面,避免超时!即:printf("%d%c",(i+j-2)%p,j==p?'\n':' ');

2.题意---叫我们自己定义加减法,我们便定义如下:

注意要取模(根据测试数据推断!)

3.不用cin,cout。用scanf,printf,这样输入输出更快。

代码:

#include 
using namespace std;

typedef long long LL;
int main(){
	int T,p;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&p);
		for(int i=1;i<=p;i++){
			for(int j=1;j<=p;j++){
				printf("%d%c",(i+j-2)%p,j==p?'\n':' ');
				//此处将判断移入到上一行的printf中,这样可避免超时... 
			}
		} 
		for(int i=1;i<=p;i++){
			for(int j=1;j<=p;j++){
				printf("%d%c", ((i-1)*(j-1))%p ,j==p?'\n':' ');
				
			}
		} 
	}
	return 0;
}
//getchar函数是字符输入函数,putchar代表是单个字符输出函数

 

你可能感兴趣的:(algorithm)