首页 > 技术文章 > HDU6440 Dream 2018CCPC网络赛-费马小定理

Cwolf9 2018-08-25 22:18 原文

(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:Portal传送门

 原题目描述在最下面。
 给定一个素数p,要求定义一个加法运算表和乘法运算表,使的\((m+n)^p=m^p+n^p(0\le m,n\lt p)\)成立。

Solution:

 费马小定理:\(a^{p-1} = 1\;mod\;p\)(\(p\)是素数)
 所以 \(a^p \;mod\; p = a^{p-1} \times a \;mod \;p = a \;mod \;p\)
 所以有 \((a+b)^p \; mod\;p= a + b \; mod\; p = a^p + b ^p \;mod\;p\)
 因此上式子成立。

AC_Code:

#include<bits/stdc++.h>
#define mme(a,b) memset((a),(b),sizeof((a))) 
using namespace std;
typedef unsigned long long LL;
const int N = 2e5 + 7;
const int M = 1e5 + 7;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int add(int x, int y, int mod) {
    int ret = x + y;
    if(ret >= mod) ret -= mod;
    return ret;
}
int multiply(int x, int y, int mod) {
    int ret = x * y;
    if(ret >= mod) ret %= mod;
    return ret;
}
int main() {
    int tim, n;
    scanf("%d", &tim);
    while(tim--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            printf("%d", i);
            for (int j = 1; j < n; j++) printf(" %d", add(i, j, n));
            puts("");
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++){
              printf("%d%c", multiply(i, j, n), j == n - 1 ? '\n' : ' ');
            }
        }
    }
    return 0;
}

Problem Description:

这里写图片描述

推荐阅读