zoukankan      html  css  js  c++  java
  • 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=1725. 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,ap1a,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,kZ} equal to {k|0<k<p,kZ}. 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=11=1. On the other hand, 02=00=012=11=102+12=0+1=1.
    They are the same.
     
    Input
    The first line of the input contains an positive integer T(T30) 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(1jp) integer of i-th(1ip) line denotes the value of (i1)+(j1). The j-th(1jp) integer of (p+i)-th(1ip) line denotes the value of (i1)(j1).
     
    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;
    }
    View Code
  • 相关阅读:
    ubuntu 搭建 php 环境
    【转】送给和我一样曾经浮躁过的PHPer程序猿,希望有帮助
    thinkphp iis下去掉index.php
    windows定时执行PHP的技巧
    js 生成随机数字的方法
    Linux下crontab命令的用法
    收藏下(设为收藏,设为首页)
    C#扩展方法的理解
    Win7 访问共享时输入正确密码仍然提示密码错误
    SQL Server 获取插入记录后的自动编号ID
  • 原文地址:https://www.cnblogs.com/albert-biu/p/9535229.html
Copyright © 2011-2022 走看看