2018 CCPC网络赛 Dream (费马小定理)

Dream

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

p是一个质数,由费马小定理,a∈Z,所以a^p≡ a mod p

费马小定理:假如p是质数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p),例如:假如a是整数,p是质数,则a,p显然互质(即两者只有一个公约数1),那么我们可以得到费马小定理的一个特例,即当p为质数时候, a^(p-1)≡1(mod p)。

所以对于0≤x,y≤p,

所以将m+n定义为(m+n)%p

将m*n定义为m*n%p

易证满足集合相等的约束

#include "bits/stdc++.h"

using namespace std;

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
#define ull unsigned long long
#define ll long long

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t, p;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &p);
        int flag;
        for (int i = 0; i < p; i++) {
            flag = 0;
            for (int j = 0; j < p; j++) {
                if (flag) cout << " ";
                cout << (i + j) % p ;
                flag = 1;

            }
            cout<<endl;
        }
        for (int i = 0; i < p; i++) {
            flag = 0;
            for (int j = 0; j < p; j++) {
                if (flag) cout << " ";
                cout << (i * j) % p ;
                flag = 1;

            }
            cout<<endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/albert-biu/p/9535229.html

时间: 2024-07-28 19:33:24

2018 CCPC网络赛 Dream (费马小定理)的相关文章

hdu6440 Dream 2018CCPC网络赛C 费马小定理+构造

题目传送门 题目大意: 给定一个素数p,让你重载加法运算和乘法运算,使(m+n)p=mp+np,并且 存在一个小于p的q,使集合{qk|0<k<p,k∈Z} 等于集合{k|0<k<p,k∈Z}. 然后输出两个矩阵,第一个矩阵输出i+j的值,第二个矩阵输出i*j的值.(题意好难懂,你们怎么都看懂了!!) 思路: 由费马小定理得到,当p是质数的时候,ap-1 ≡ 1(mod p),两边同乘以a,也就是说当ap和a在取模p的时候相等 所以(m+n)p=m+n=mp+np(乘法为x*x%p

HDU6440 Dream(费马小定理+构造) -2018CCPC网络赛1003

题意: 给定素数p,定义p内封闭的加法和乘法,使得$(m+n)^p=m^p+n^p$ 思路: 由费马小定理,p是素数,$a^{p-1}\equiv 1(mod\;p)$ 所以$(m+n)^{p}\equiv (m+n)(mod\;p)$ $m^{p}\equiv m(mod\;p)$ $n^{p}\equiv n(mod\;p)$ 所以在模意义下,有$(m+n)^p=m^p+n^p$ 代码: #include<iostream> #include<cstdio> #include&

2018 CCPC网络赛 Dream&amp;&amp;Find Integer

首先这场比赛打得很难受,因为是第一次打网络赛而且也是比较菜的那种,所以对这场网络赛还是挺期待和紧张的,但是在做题的过程中,主要的压力不是来自于题目,更多的来自于莫干山...从12.40-2.00所有的题目都不判了,中间也就写了1003和1004但是都不知道结果就很难受, 然后一直不判就已经没什么看其他题的兴趣了,然后上床休息了一会,直到说杭电的评测机好了,之后才上去继续做题.然后..一直在写1001和1009..后面也没有写出来..直到比赛结束 首先说下1004 签到题竟然写了这么久,而且用了c

HDU6440 Dream 2018CCPC网络赛-费马小定理

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 ?原题目描述在最下面. ?给定一个素数p,要求定义一个加法运算表和乘法运算表,使的\((m+n)^p=m^p+n^p(0≤m, n<p)\)成立. Solution: ?费马小定理:\(a^{p-1} = 1 mod p(p是素数)\) ?所以 \(a^p \;mod\; p = a^{p-1} \times a \;mod \;p = a

【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N) and each time a child is inv

2018 CCPC网络赛

2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物品,问最终赚的钱的最大值. solution 用两个堆来维护,一个堆维护已经找到卖家的,一个堆维护还没找到卖家的. 对于第\(i\)个地点,在已经找到卖家的堆里找出卖的钱的最小值,如果最小值小于\(a_i\),则将卖家换成\(i\),然后将原来的卖家放到没找到卖家的那里:如果最小值对于\(a_i\)

[2018 CCPC 网络赛] 部分题解 (待补充)

真香爬山预警 1003 - Dream 题目链接 如题,p是一个质数,那么由费马小定理  a^p ≡ a (mod p)  ( a ∈ Z )  可得 只需将 加法定义为 (m + n) % p 乘法定义为 (m * n) % p 即可   1 #include <iostream> 2 #include <bits/stdc++.h> 3 using namespace std; 4 int main() 5 { 6 int t,p; 7 scanf("%d"

题解报告:hdu 6440 Dream(费马小定理+构造)

解题思路:给定素数p,定义p内封闭的加法和乘法运算(运算封闭的定义:若从某个非空数集中任选两个元素(同一元素可重复选出),选出的这两个元素通过某种(或几种)运算后的得数仍是该数集中的元素,那么,就说该集合对于这种(或几种)运算是封闭的.),使得等式恒成立. 由费马小定理可得,∴,则. ∴在模p的意义下,恒成立,且加法运算与乘法运算封闭. 即乘法运算满足. AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 int t,p; 4 in

hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                         (全题文末) 知识点: 整数n有种和分解方法. 费马小定理:p是质数,若p不能整除a,则 a^(p-1) ≡1(mod p).可利用费马小定理降素数幂. 当m为素数,(m必须是素数才能用费马小定理) a=2时.(a=2只是题中条件,a可以为其他值) mod m =  *      //  k=