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

举报
用户已注销 发表于 2021/11/19 02:04:41 2021/11/19
【摘要】 目录 一,拉格朗日四平方和定理 二,证明过程 三,推论 四,OJ实战 CSU 1404: Four-square Theorem 力扣 279. 完全平方数 一,拉格朗日四平方和定理 每个正整数均可表示为4个整数的平方和   二,证明过程 1,欧拉恒等式 2, 3,任意素数都可以表...

目录

一,拉格朗日四平方和定理

二,证明过程

三,推论

四,OJ实战

CSU 1404: Four-square Theorem

力扣 279. 完全平方数


一,拉格朗日四平方和定理

每个正整数均可表示为4个整数的平方和

 

二,证明过程

1,欧拉恒等式

(a^2+b^2+c^2+d^2)(x^2+y^2+z^2+w^2)= (ax+by+cz+dw)^2+(ay-bx+cw-dz)^2+(az-bw-cx+dy)^2+(aw+bz-cy-dx)^2

2,\exists x,y,\: x^2+y^2+1=mp,\, 0<m<p

3,任意素数都可以表示为四平方和

 

三,推论

形如4^a * (8k+7)的数不能表示成三平方和,其他形式的数都可以表示成三平方和。

 

四,OJ实战

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 a, b, c, d 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个平方和的数

然后再暴力枚举

代码:


  
  1. #include<iostream>
  2. #include<math.h>
  3. #include<stdio.h>
  4. using namespace std;
  5. bool g(int n)
  6. {
  7. while (n % 4 == 0)n /= 4;
  8. return n % 8 == 7;
  9. }
  10. void f(int n)
  11. {
  12. for (int a = int(sqrt(n));; a--)
  13. {
  14. n -= a*a;
  15. if (n && g(n))
  16. {
  17. n += a*a;
  18. continue;
  19. }
  20. for (int b = 0; b*b <= n; b++)
  21. {
  22. n -= b*b;
  23. for (int c = 0; c <= b && c*c <= n; c++)
  24. {
  25. n -= c*c;
  26. int d = int(sqrt(n));
  27. if (n == d*d)
  28. {
  29. printf("%d %d %d %d\n", a, b, c, d);
  30. return;
  31. }
  32. n += c*c;
  33. }
  34. n += b*b;
  35. }
  36. }
  37. }
  38. int main()
  39. {
  40. int t, n;
  41. scanf("%d", &t);
  42. while (t--)
  43. {
  44. scanf("%d", &n);
  45. f(n);
  46. }
  47. return 0;
  48. }

力扣 279. 完全平方数

题目:

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3 
解释: 12 = 4 + 4 + 4.
示例 2:

输入: n = 13
输出: 2
解释: 13 = 4 + 9.

思路一:

动态规划,枚举n的平方和组成中可能出现的值,只要枚举一个即可化为子问题。

代码:
 


  
  1. class Solution {
  2. public:
  3. int numSquares(int n) {
  4. static map<int,int>m;
  5. if(m[n])return m[n];
  6. m[n]=n;
  7. for(int i=1;i*i<=n;i++)m[n]=min(m[n],numSquares(n-i*i)+1);
  8. return m[n];
  9. }
  10. };

时间复杂度:O(n^(3/2))

思路二:

用拉格朗日四平方和定理,所有的数都是四平方和。

判断一个数是不是平方数,如果不是的话,是不是平方和,如果不是的话,是不是三平方和,如果不是三平方和,那就是4。

先算出所有的平方,以及所有的平方和,然后用双指针扫描一遍看有没有加起来是n的。

时间复杂度O(n)

思路三:

用四平方和定理的推论,形如4^a * (8k+7)的数不能表示成三平方和,其他形式的数都可以表示成三平方和

只需要判断一个数是不是平方和即可。

用双指针的话,时间复杂度O( sqrt(n))

暴力枚举平方和的话,O(n)


  
  1. bool g(int n)
  2. {
  3. while (n % 4 == 0)n /= 4;
  4. return n % 8 == 7;
  5. }
  6. class Solution {
  7. public:
  8. int numSquares(int n) {
  9. if(g(n))return 4;
  10. if(int(sqrt(n))*int(sqrt(n))==n)return 1;
  11. for(int i=0;i<=100;i++)for(int j=0;j<=i;j++)if(n==i*i+j*j)return 2;
  12. return 3;
  13. }
  14. };


 

 

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/115683823

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。