1. 程式人生 > >CSU 1404: Four-square Theorem(拉格朗日四平方和定理)

CSU 1404: Four-square Theorem(拉格朗日四平方和定理)

題目:

Description

Lagrange’s four-square theorem states that any natural number can be represented as the sum of four integer squares: n = a2 + b2 + c2 + d2. For example, 3, 31 and 310 can be represented as the sum of four squares as follows: 3 = 12 + 12 + 12 + 02, 31 = 52 + 22 + 12 + 12, 310 = 172 + 42 + 22
 + 12.
Given an integer n, represent it by the sum of four integer squares.This result may be helpful: Any integer n which is not of the form 4a(8m + 7) can be written as a sum of three squares, where a and m are arbitrary non-negative integers. For illustration, 7 = 40 * (8 * 0 + 7), so 7 can not be written as a sum of three squares.

Input

The first line contains the number of test cases T (1 <= T <= 104).For each test case, there is only one line with an integer n (1 <= n <= 109).

Output

For each test case, output four integers abcd separated by a single space which satisfy n2 = a2 + b2 + c2 + d2. If there are multiple solutions, anyone will be accepted.

Sample Input

5
1
7
7
10
10

Sample Output

1 0 0 0
1 1 1 2
1 2 1 1
1 0 3 0
2 1 1 2

思路:

本來我的思路是利用尤拉四平方和恆等式,不過並沒有什麼效果,遇到8m+7型的大素數就直接變成暴力枚舉了。

這個題目主要是根據給的提示,把不能表示成3個平方和的數拆分成一個儘量大的平方和加上一個可以表示成3個平方和的數

然後再暴力列舉

程式碼:

#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;

bool g(int n)
{
	while (n % 4 == 0)n /= 4;
	return n % 8 == 7;
}

void f(int n)
{
	for (int a = int(sqrt(n));; a--)
	{
		n -= a*a;
		if (n && g(n))
		{
			n += a*a;
			continue;
		}
		for (int b = 0; b*b <= n; b++)
		{
			n -= b*b;
			for (int c = 0; c <= b && c*c <= n; c++)
			{
				n -= c*c;
				int d = int(sqrt(n));
				if (n == d*d)
				{
					printf("%d %d %d %d\n", a, b, c, d);
					return;
				}
				n += c*c;
			}
			n += b*b;
		}		
	}	
}

int main()
{
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		f(n);
	}
	return 0;
}

/**********************************************************************
	Problem: 1404
	User: 3901140225
	Language: C++
	Result: AC
	Time:76 ms
	Memory:2036 kb
**********************************************************************/

相關推薦

CSU 1404: Four-square Theorem平方和定理

題目:DescriptionLagrange’s four-square theorem states that any natural number can be represented as the

[數學 平方和定理 Rho大整數分解] BZOJ 2904 平方和

PS linux下RAND_MAX是2^31!!TLE了快一頁 另一題4522跟著一起T #include<cstdio> #include<cstdlib> #include<ctime> #include<algorit

bzoj2876 [NOI2012]騎行川藏乘數法

輸入輸出格式 int 至少 lin 答案 就是 math spa space 題目描述 蛋蛋非常熱衷於挑戰自我,今年暑假他準備沿川藏線騎著自行車從成都前往拉薩。川藏線的沿途有著非常美麗的風景,但在這一路上也有著很多的艱難險阻,路況變化多端,而蛋蛋的體力十分有限,因此在每天的

CodeForces - 813C The Tag Game乘數法,限制條件求最值

The int main fixed 方法 情況 upper typedef 題目 【傳送門】http://codeforces.com/problemset/problem/813/C 【題意】給定整數a,b,c,s,求使得 xa yb zc值最大的實數 x,y,z

bzoj3453: tyvj 1858 XLkxc插值

傳送門 \(f(n)=\sum_{i=1}^ni^k\),這是自然數冪次和,是一個以\(n\)為自變數的\(k+1\)次多項式 \(g(n)=\sum_{i=1}^nf(i)\),因為這東西差分之後是\(f\),所以這是一個\(k+2\)次多項式 同理最後我們要求的也是一個\(k+3\)次多項式 \(

P3270 [JLOI2016]成績比較插值

傳送門 挺神仙的啊…… 設\(f[i][j]\)為考慮前\(i\)門課程,有\(j\)個人被\(B\)爺碾壓的方案數,那麼轉移為\[f[i][j]=\sum_{k=j}^{n-1}f[i-1][k]\times {k \choose k-j}\times {n-1-k \choose r[i]-1-(k-

P4463 [國家集訓隊] calc插值

傳送門 設\(dp[i][j]\)為考慮\(i\)個數,其中最大值不超過\(j\)的答案,那麼轉移為\[dp[i][j]=dp[i-1][j-1]\times i\times j+dp[i][j-1]\] 即最大值不超過\(j-1\)的答案加上最大值剛好為\(j\)的答案,乘上\(i\)是因為\(j\)可以

牛客多校第一場 F. Sum of Maximum插值

題目描述 Given a1, a2, ..., an, find  modulo (109+7). 輸入描述: The input consists of several test cases and is terminated by end-of-file. T

Lagrange multiplier method 乘數法

The Lagrange multiplier method is used to solve the problem that find the extremum of a function z=f(x,y), given ϕ(x,y)=0. simpl

羅爾定理中值定理和柯西中值定理和用他們證明不等式、

已知f(x), F(x)在閉區間[a,b]上連續,在(a,b)上可導 羅爾定理 如果f(a)=f(b), 則必定存在 a<ξ<b, 令 f’(ξ)=0 拉格朗日中值定理 必定存在 a<ξ<b, 令 f’(ξ) = ( f(b) - f(a

MT【275】中值定理

已知$0<x_1<c<x_2<e^{\frac{3}{2}},$且$\dfrac{1-ln(c)}{c^2} = \dfrac{x_1ln(x_2)-x_2ln(x_1)}{x_1x_2(x_2-x_1)}$, 證明:$c^2<x_1x_2$ 由題意,結合拉格朗日中值定理知

深入理解乘子法Lagrange Multiplier) 和KKT條件

比較 log lan 條件 出了 net csdn art blank 這篇將拉格朗日函數比較全面,其中明確給出了拉格朗日函數,拉格朗日乘子的定義 深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT條件深入理解拉格朗日乘子法(Lagrange Mu

尋找“最好”3——函數和泛函的乘數法

big 我們 -a 都是 使用 col 可能 條件 解釋 拉格朗日乘數法   大多數的優化問題都會加入特定的約束,而不僅僅是指定起點和終點,此時需要更好的辦法去解決優化問題,拉格朗日乘數法正是一種求約束條件下極值的方法。   簡單地說,拉格朗日乘數法(又稱為拉格朗日乘數法)

2018牛客網暑期ACM多校訓練營第一場F.Sum of Maximum(組合數學+插值)

題目連結:https://www.nowcoder.com/acm/contest/139#question 轉載出處:http://tokitsukaze.live/2018/07/19/2018niuke1.F/   程式碼: #include <bits/

[LOJ#2473][九省聯考2018]祕密襲擊樹形DP+生成函式+線段樹合併+插值

Address 洛谷P4365 BZOJ5250 LOJ#2473 The First Step - 轉化 簡版題意:給定一棵點帶權樹,求樹上所有大小大於 k

詳解SVM系列對偶性

拉格朗日函式有什麼用? 在約束最優化問題中,常常利用拉格朗日對偶性將原始問題轉換為對偶問題,通過解對偶問題而得到原始問題的解。 原始問題: 假設 f (

對偶性KKT

sum 問題 clas 4.4 我們 line 約束 分別是 lin 1. 前言 在約束最優化問題中,常常利用拉格朗日對偶性將原始問題轉化為對偶問題,通過求解對偶問題獲得原始問題的解。該方法應用在許多統計學方法中,如最大熵模型、支持向量機。 2. 原始問題 假設\(f(x)

【XSY1537】五顏六色的幻想鄉矩陣樹定理+高斯消元+插值

題意:有nn個點,mm條有顏色的邊,顏色為紅色藍色和綠色,對於所有滿足r+b+g=n−1r+b+g=n−1的三元組(r,b,g)(r,b,g),求恰有rr條紅色的邊,bb條藍色的邊,gg條綠色的邊的生成樹個數。 題解: 生成樹計數基本上就是矩陣樹定理啦。

MATLAB——數值插值之Lagrange插值法

 由於MATLAB中沒有現成的拉格朗日插值命令,因此我們可以根據Lagrange插值法的定義編寫一個Lagrange插值命令。    定義: 給定n個插值節點x1,x2,...,xn和對應的函式值y1,y2,...yn,利用拉格朗日插值多項式公式(注:k從0到n累加),其

乘子法有約束優化問題

                     拉格朗日乘子法(Lagrange Multiplier)和KKT(Karush-Kuhn-Tucker)條件是求解約束優化問題的重要方法,在有等式約束時使用拉格朗日乘子法,在有不等約束時使用KKT條件。前提是:只有當目標函式為凸函式時,使用這兩種方法才保證求得的是最優