NEU 1440 The minimum square sum (平方剩余和欧拉准则)

若p=2或p=4*k+1 则p可以表成两平方数的和的形式 (欧拉和费马已证明,并且有求的方法) 所以答案是p

若p=4*k+3 设a^2=n(mod p) (n!=0)  可以证明不存在b,b^2=p-n(mod p) 即若n是p的平方剩余 则p-n不是p的平方剩余

证明:因为a^2=n(mod p) 所以由欧拉准则 得n^((p-1)/2)=1(mod p)

若b^2=-n(mod p) 那么(-n)^((p-1)/2)=1(mod p)

左边把符号提出来 得(-1)^((p-1)/2)*n^((p-1)/2)

因为p是4*k+3型的 所以(p-1)/2是奇数 所以左边不可能等于1 假设与事实矛盾所以

因此a^2=0(mod p) b^2=0(mod p) 所以答案是2*p*p

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long p;
    while(scanf("%lld",&p)==1)
    {
        if(p==2||p%4==1)
            printf("%lld\n",p);
        else
            printf("%lld\n",p*p*2);
    }
    return 0;
}

NEU 1440 The minimum square sum (平方剩余和欧拉准则)

时间: 2024-08-29 08:05:02

NEU 1440 The minimum square sum (平方剩余和欧拉准则)的相关文章

黑龙江省赛The minimum square sum

题目 The minimum square sum Time Limit 1000ms Memory Limit 65536K description Given a prime p (p<108),you are to find min{x2+y2},where x and y belongs to positive integer, so that x2+y2=0 (mod p). input Every line is a p. No more than 10001 test cases.

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 这道题的要求是在m*n

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to rea

LeetCode: Minimum Path Sum 解题报告

Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. SOLUTION 1: 相当基础

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题分析: 每次只能向下或者向

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or r

leetcode_64题——Minimum Path Sum(动态规划)

Minimum Path Sum Total Accepted: 38669 Total Submissions: 120082My Submissions Question Solution Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Not