2018中国大学生程序设计竞赛 - 网络选拔赛(hdu6440 Dream)

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<q<p) to make the set {qk|0<k<p,k∈Z} equal to {k|0<k<p,k∈Z}. What's more, the set of non-negative integers less than p ought to be closed under the operation of your definitions.
 

Hint

Hint for sample input and output:
From the table we get 0+1=1, and thus (0+1)2=12=1⋅1=1. On the other hand, 02=0⋅0=0, 12=1⋅1=1, 02+12=0+1=1.
They are the same.

Input

The first line of the input contains an positive integer T(T≤30) indicating the number of test cases.

For every case, there is only one line contains an integer p(p<210), described in the problem description above. p is guranteed to be a prime.

Output

For each test case, you should print 2p lines of p integers.

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).

Sample Input

1

2

Sample Output

0 1

1 0

0 0

0 1

题意:给定的p是素数,要求给定一个加法运算表和乘法运算表,使(m+n)^p=m^p+n^p(0≤m,n<p)。且存在一个数w,w^t(0<=t<p)在(0到<p)之间一一对应。

思路:

官方题解:

费马小定理:a^(p−1)=1 mod p (p是素数)
所以 a^p mod p = a^(p−1) × a mod p = a mod p
所以有 (a+b)^p mod p = (a+b) mod p = a^p + b^p mod p
因此上式子成立。

另一种解法:

其实只要重定义加法的矩阵全为0,就能保证(m+n)^p=n^p+m^p,然后只要让乘法强行满足题目条件,比如定义1*1=2,1*2=3,1*3=4...这样强行让1^t满足就行

代码:

官方做法:

#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;

int main()
{
    int T_T;
    ll p;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%lld",&p);
        for(int i=0;i<p;i++)
        {
            for(int j=0;j<p;j++)
            {
                cout<<(i+j)%p<<" ";
            }
            cout<<endl;
        }
        for(int i=0;i<p;i++)
        {
            for(int j=0;j<p;j++)
            {
                cout<<(i*j)%p<<" ";
            }
            cout<<endl;
        }
    }
}

另一种解法:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define qq cout<<"1";
#define pp cout<<"0";
using namespace std;
const int maxn=200010;
const ll mo=1e9+7;
int n,m,p;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>p;
        for(int i=0;i<p;i++)
        {
        for(int j=0;j<p;j++)
        {
          if(j) cout<<" ";
          pp
        }
        cout<<endl;
        }
        for(int i=0;i<p;i++)
        {for(int j=0;j<p;j++)
        {
          if(j) cout<<" ";
          if(i==1&&j!=p-1&&j>0) cout<<i+j;
          else if(i==1&&j==p-1&&j>0) qq
          else if(j==1&&i!=p-1&&i>0) cout<<i+j;
          else if(j==1&&i==p-1&&i>0) qq
          else pp
        }
        cout<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/snayf/article/details/82083890
今日推荐