欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

费马小定理

程序员文章站 2022-06-08 11:06:45
...

Dream

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 908    Accepted Submission(s): 130
Special Judge

 

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

 

 

Source

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

费马小定理:

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

 

Statistic | Submit | Discuss | Note

费马小定理:

在数论中,欧拉定理,(也称费马-欧拉定理)是一个关于同余的性质。欧拉定理表明,若n,a为正整数,且n,a互质,则:费马小定理

当n为质数时,上式费马小定理变成费马小定理

费马小定理推论:

a^p % c=a^(a%(c-1)) %c

如果p可以写成p=k(c-1)+d, 即d=a%(c-1)
那么,
a^p %c
=a^(k(c-1)+d) % c
=a^(k(c-1))*a^d %c
=a^(c-1) * …a^(c-1) * a^d % c (…是k个)
=a^d %c
所以,
a^p % c
=a^(a%(c-1)) %c

2^x mod n = 1【费马小定理】

 费马小定理的运用:

n为偶数时 不会存在x 使得 2 的 x次方 对n取余为1;

n为1时也不会存在;

n为非1奇数时,则gcd(n,2)==1,故必存在一值x使得 2 的 x次方 对n取余为1,暴力枚举即可。

根据同余的性质可得:同余的性质可看这篇博客https://blog.csdn.net/qq_37891604/article/details/82079191

费马小定理

#include<bits/stdc++.h>
using namespace std;
const int maxn =1e5+5;
typedef long long LL;
 
int main()
{
 
    int p;
    int T; scanf("%d",&T);
    while(T--){
        scanf("%d",&p);
        for(int i=0;i<p;++i){
            for(int j=0;j<p;++j){
                printf("%d%c",(i+j)%p,j==p-1?'\n':' ');
            }
        }
        for(int i=0;i<p;++i){
            for(int j=0;j<p;++j){
                printf("%d%c",(i*j%p),j==p-1?'\n':' ');
            }       
        }
    }
    return 0;
}

比赛的时候自己硬生生把他它做成了构造题

#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;
}