HDU 6400(费马小定理)

博客围绕一个数论问题展开,给定质数p,需重定义加法和乘法,使(m+n)p=mp+np对[0,p - 1]内的n、m成立。分析指出可利用费马小定理,在加法和乘法过程中取模数p得到答案,最后给出代码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题面:

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,p=0ap−1⋅a,p>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,让你重定义加法和乘法,使得对于任意的n,m属于[0,p-1],满足式子,最后,对于1到p行,你将要输出第i行与第j列的数相加的结果(其中);对于第p+1行到2p行,你将要输出第i行与第j列相乘的结果,(其中)

题目分析:

    这是一个被隐藏得很深的数论问题。题意相对来说比较难懂,但是明白题意之后,其实这个题目做起来并不困难。

    首先题目要求我们满足式子,思考题目中给我们的p是素数,因此我们不难想到费马小定理,故对于等式两边同时mod p可得:

    

    因此我们只需要在加法和乘法的过程中取个模数p即是最终答案。

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int T,p;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&p);
        for(int i=0;i<p;i++){
            for(int j=0;j<p-1;j++){
                printf("%d ",(i+j)%p);
            }
            printf("%d\n",(i+p-1)%p);
        }
        for(int i=0;i<p;i++){
            for(int j=0;j<p-1;j++){
                printf("%d ",(i*j)%p);
            }
            printf("%d\n",(i*(p-1))%p);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Chen-Jr/p/11007220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值