【2018 CCPC网络赛】1003 - 费马小定理

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6440

 

这题主要是理解题意;

题意:定义一个加法和乘法,使得 (m+n)p = mp+np;

   其中给定 p 为素数,m,n 为小于p的数;

 

   费马小定理:am-1 ≡ 1(mod p); 

   故有 a≡ a(mod p), 同理(a+b)m = a+b(mod p) = a+ b;  

   所以原等式恒成立,不需要定义特别的加法和乘法,只需在原来的基础上取模即可;

 

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 int T, p;
 6 
 7 int main()
 8 {
 9     scanf("%d",&T);
10     while(T--)
11     {
12         cin>>p;
13         for(int i=0; i<p; i++) {
14             for(int j=0; j<p-1; j++) 
15                 printf("%d ", (i+j)%p);
16             printf("%d\n", (i+p-1)%p);
17         }
18         for(int i=0; i<p; i++) {
19             for(int j=0; j<p-1; j++) 
20                 printf("%d ", i*j%p);
21             printf("%d\n", i*(p-1)%p);
22         }
23     }
24     
25     return 0;
26 }
posted @ 2018-08-27 11:11  liubilan  阅读(213)  评论(0编辑  收藏  举报