传送门

题面:

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

HDU 6400(费马小定理)相关推荐

  1. hdu 4549 M斐波那契数列(费马小定理 + 二分快速幂 + 矩阵快速幂)

    M斐波那契数列                                                                           Time Limit: 3000/1 ...

  2. HDU - 5667 Sequence(矩阵快速幂+费马小定理降幂)

    题目链接:点击查看 题目大意:给出函数f(x): 现给出n,a,b,c,mod,求f(n)对mod取模后的结果 题目分析:这个题目相对于前几个题来说稍微加大了点难度,但还是挺水的一个题,首先我们可以对 ...

  3. HDU 4704 Sum (费马小定理)

    题意 给定一个N,设S(K)表示x1 + x2 + -- + xk = N的解的个数(xi ∈ Z+),求SUM( S(i), 0 <= i <= N )  mod  109+7. 思路 ...

  4. HDU 4549 M斐波那契数列(矩阵快速幂费马小定理)

    ps:今天和战友聊到矩阵快速幂,想到前几天学长推荐去刷矩阵专题,挑了其中唯一一道中文题,没想到越过山却被河挡住去路... 题目链接:[kuangbin带你飞]专题十九 矩阵 R - M斐波那契数列 T ...

  5. 【ACM】杭电OJ 4704 Sum (隔板原理+组合数求和公式+费马小定理+快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4704 1.隔板原理 1~N有N个元素,每个元素代表一个1.分成K个数,即在(N-1)个空挡里放置(K-1)块隔板 ...

  6. 数论杂谈(欧拉定理与费马小定理结论与应用)

    文章目录 欧拉定理: 欧拉定理性质: 扩展欧拉定理: 费马小定理: 指数循环节 费马大定理 逆元: 例题 原根 定义: 原根存在条件 例题 快速幂 代码 矩阵快速幂 原理: 代码: 欧拉定理: aφ( ...

  7. 三个重要的同余式——威尔逊定理、费马小定理、欧拉定理 + 求幂大法的证明

    一.威尔逊定理 若p为质数,则 p|(p-1)!+1 亦:(p-1)! ≡ p-1 ≡ -1(mod p) 例题: HDU 2973 YAPTCHA (威尔逊定理及其逆定理) 解题报告见http:// ...

  8. 7. 数论四大定理(威尔逊定理、欧拉定理、费马小定理、孙子定理)

    一.准备工作 点击查看数论基础知识 二.威尔逊定理 威尔逊定理给出了判定一个自然数是否为素数的充分必要条件.但是由于阶乘是呈爆炸增长的,其结论对于实际操作意义不大. 1. 定理及其变形 当且仅当p为素 ...

  9. 数论四大定理(欧拉定理、费马小定理、中国剩余定理、威尔逊定理)

    前置知识 同余 假设 a,ba,ba,b 都是整数,如果 nnn 是一个正整数,且存在整数 kkk 使得 a−b=k×na−b=k \times na−b=k×n,则称 a,ba,ba,b 模 nnn ...

最新文章

  1. Kernel tasklet
  2. 2019-7-29 考试总结
  3. Service生命周期
  4. [PKUWC2018][loj2541]猎人杀
  5. 代码分析 | 单细胞转录组Normalization详解
  6. 为什么mvc里面的ModelState.IsValid一只都是true
  7. [Python学习]错误篇二:切换当前工作目录时出错——FileNotFoundError: [WinError 3] 系统找不到指定的路径...
  8. LeetCode----两数之和
  9. android源码 分享1
  10. Lanproxy任意文件读取漏洞复现(CVE-2021-3019)
  11. Python 通过打码平台实现验证码
  12. 微信小程序开发 - 模板与配置
  13. PHP编写简单的注册登录页面
  14. 除了用jenkins,还有什么方法可实现持续集成?
  15. SQL语句中case、when、then的使用
  16. gltf骨骼动画解析笔记
  17. 电脑安装虚拟机网络适配器上面没有虚拟机的网络
  18. 时间序列分析——基于R语言案例数据课后数据
  19. bert系列第一篇: bert进行embedding
  20. IEEE 802.15.4协议完整中文版 - 4.2 IEEE 802.15.4 WPAN 的组件

热门文章

  1. 金鉴实验室 PCBA切片分析 PCB检测
  2. 如何强制卸载阿里云盾(安骑士)监控及屏蔽云盾IP检测附带教程
  3. Resource Path Location Type Project configuration is not up-to-date with pom.xml. Run Ma
  4. 蓝桥杯倒计时 | 倒计时6天
  5. 如何提升高速滑环的可靠性
  6. Arduino安装教程
  7. Linux - BBS项目学习 - 项目的完整部署(LNM+Python+Django+uwsgi+redis)
  8. python中numpy是什么意思_python中numpy是什么
  9. 用Python制作一个相册播放器(附源码)
  10. STM32西门子PLC源码新版原创