HDU 6440 Dream(费马小定理)

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

文章地址:http://henuly.top/?p=832

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 = m p + n p , where m,n,p are real numbers. Let’s call it “Beginner’s Dream”.

For instance, ( 1 + 4 ) 2 = 5 2 = 2 5 , but 1 2 + 4 2 = 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 = m p + n p

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 = m p + n p is a valid identity for all non-negative integers m,n less than p. Power is defined as

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

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

扫描二维码关注公众号,回复: 2992531 查看本文章

Sample Output:

0 1
1 0
0 0
0 1

Hint:

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

题目链接

费马小定理:若p是素数且a是整数则 a p ≡a(mod p)

( m + n ) p ≡(m+n)(mod p)

m p ≡m(mod p)

n p ≡n(mod p)

所以 ( m + n ) p ( m + n ) ( m o d p ) m p + n p m ( m o d p ) + n ( m o d p )

( m + n ) ( m o d p ) m ( m o d p ) + n ( m o d p )

这个式子重新定义的新运算是在%p下进行的,那么把新的加法和乘法运算也变成再%p下进行的运算即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]) {
    int T;
    scanf("%d", &T);
    for (int Case = 1, p; Case <= T; ++Case) {
        scanf("%d", &p);
        for (int i = 0; i < p; ++i) {
            for (int j = 0; j < p; ++j) {
                printf("%d ", (i + j) % p);
            }
            printf("\n");
        }
        for (int i = 0; i < p; ++i) {
            for (int j = 0; j < p; ++j) {
                printf("%d ", (i * j) % p);
            }
            printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Tony5t4rk/article/details/82115627