ZZW's Blog

Step by step!


  • 首页

  • 关于

  • 标签12

  • 分类6

  • 归档22

  • 搜索

费马小定理例题汇总

发表于 2019-02-09 更新于 2019-07-07 分类于 算法训练
本文字数: 2.6k 阅读时长 ≈ 2 分钟

hdu 6440 Dream

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=25 $, but $ 1^2+4^2=17\neq25$.
Moreover, $ \sqrt{9+16}= \sqrt{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<q<p)$ to make the set ${q^k|0<k<p,k\in\mathbb{Z}}$ equal to ${k|0<k<p,k\in\mathbb{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=1^2=1 \cdot 1=1$. On the other hand, $0^2=0\cdot0=0, 1^2=1\cdot1=1, 0^2+1^2=0+1=1$.
They are the same.

Input

The first line of the input contains an positive integer $T(T\leq 30) $ indicating the number of test cases.
For every case, there is only one line contains an integer $p(p<2^{10})$, 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\leq j \leq p)$ integer of i-th$(1 \leq i \leq p)$ line denotes the value of (i−1)+(j−1). The j-th $(1\leq j \leq p)$ integer of (p+i)-th $(1\leq i \leq p)$ line denotes the value of $(i−1) \cdot(j−1)$.

Sample Input

1
2
1
2

Sample Output

1
2
3
4
0 1
1 0
0 0
0 1

思路:

给定素数p,定义p内封闭的加法和乘法运算,使得等式$(m+n)^p = m^p + n^p\;(0 \leq m,n<p) $ 恒成立。
由费马小定理可得对 $\forall \; x \in \mathbb{Z} $ 都有$x^p\equiv x\;(mod\;p)$,其中p是素数,则 $ m^p\equiv m\;(mod\;p),n^p\equiv n\;(mod\;p),(m+n)^p\equiv(m+n)\;(mod\;p)$,
所以在模p的意义下,
$ (m+n)^p = m^p + n^p(0 \leq m,n<p),m^p \cdot n^p \equiv m\cdot n\;(mod\;p)$ 恒成立,即加法运算与乘法运算封闭。
备注:运算封闭的定义:若从某个非空数集中任选两个元素(同一元素可重复选出),选出的这两个元素通过某种(或几种)运算后的得数仍是该数集中的元素,那么,就说该集合对于这种(或几种)运算是封闭的。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<bits/stdc++.h>
using namespace std;
int t,p;
int main(){
while(cin >> t){
while(t--){
cin >> 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;
}
  • 本文作者: wzomg
  • 本文链接: http://blog.wzomg.cn/posts/c279d4a4.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
费马小定理
(扩展)欧拉定理例题汇总
Hello World
  • 文章目录
  • 站点概览
wzomg

wzomg

A thousand miles begins with a single step.
22 日志
6 分类
12 标签
GitHub E-Mail 博客园 简书
Creative Commons
  1. 1. hdu 6440 Dream
    1. 1.1. Hint
    2. 1.2. Input
    3. 1.3. Output
    4. 1.4. Sample Input
    5. 1.5. Sample Output
    6. 1.6. 思路:
    7. 1.7. AC代码:
© 2019 – 2021 wzomg  粤ICP备19066467号-1 | 113k | 1:43
0%