鱼C论坛

 找回密码
 立即注册
查看: 2398|回复: 2

题目136:考察方程x^2 - y^2 - z^2 = n何时有唯一解

[复制链接]
发表于 2016-8-27 01:48:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Singleton difference

The positive integers, x, y, and z, are consecutive terms of an arithmetic progression. Given that n is a positive integer, the equation, x2 − y2 − z2 = n, has exactly one solution when n = 20:

132 − 102 − 72 = 20


In fact there are twenty-five values of n below one hundred for which the equation has a unique solution.

How many values of n less than fifty million have exactly one solution?


题目:

正整数 x,y 和 z 是等差数列中的三个连续项。给定一个正整数 n,当 n=20 时方程 x2 − y2 − z2 = n 有唯一解:

132 − 102 − 72 = 20

事实上,一百以下共有 25 个 n 值使得上述方程有唯一解。

在 5000 万以下有多少个 n 值使得上述方程有唯一解?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-7-20 08:46:57 | 显示全部楼层
本题和135题是一样的,改下参数就好了
  1. """
  2. (z+2d)^2-(z+d)^2-z^2=n
  3. 3d^2+2dz-z^2=n
  4. (3d-z)*(d+z)=n
  5. z<3d => d > z/3
  6. """
  7. from numba import jit
  8. import numpy as np
  9. @jit
  10. def solve(target, limit):
  11.         master = np.zeros(limit+1,dtype='int16')
  12.         for z in range(1, limit):
  13.                 for d in range(z//3+1, limit):
  14.                         if (3*d-z)*(d+z)>limit:
  15.                                 break
  16.                         master[(3*d-z)*(d+z)] += 1
  17.         count = 0
  18.         for value in master:
  19.                 if value == target:
  20.                         count += 1
  21.         return count
  22. print(solve(1, 50000000))
复制代码

2544559
[Finished in 4.5s]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-18 22:38:13 | 显示全部楼层
应用OpenACC来编程。
  1. /*
  2. 答案:2544559
  3. 耗时:2.17018s (cmp 30hx)
  4. */
  5. #include <iostream>
  6. #include <cmath>
  7. #include <omp.h>
  8. #include <openacc.h>
  9. using namespace std;

  10. int main(void)
  11. {
  12.   double t = omp_get_wtime();
  13.   int nCount = 0;
  14. #pragma acc kernels loop reduction(+:nCount)
  15.   for (int n = 1; n <= 50000000; ++n)
  16.   {
  17.     int s = -1;//记录所以整数解
  18.     bool bFlag = false;
  19.     int d1 = (int)sqrt((double)n);
  20.     for (int m1 = 1; m1 <= d1; ++m1) //枚举n的因数分解
  21.     {
  22.       if (n % m1 == 0)
  23.       {
  24.         int m2 = n / m1;
  25.         if ((m1 + m2) % 4 != 0) //检查d是否是一个整数
  26.           continue;
  27.         int d = (m1 + m2) / 4;
  28.         if (m1 - d > 0)
  29.         {
  30.           if (s == -1 || s == (m1 - d))
  31.           {
  32.             s = (m1 - d);//得到一个整数解
  33.             bFlag = true;
  34.           }
  35.           else
  36.           {
  37.             bFlag = false;
  38.             break;
  39.           }
  40.         }
  41.         if (m2 - d > 0)
  42.         {
  43.           if(s == -1 || s == (m2 - d))
  44.           {
  45.             s = (m2 - d);//得到一个整数解
  46.             bFlag = true;
  47.           }
  48.           else
  49.           {
  50.             bFlag = false;
  51.             break;
  52.           }
  53.         }
  54.       }
  55.     }
  56.     if (bFlag)//检查解的唯一性
  57.       ++nCount;
  58.   }
  59.   t = omp_get_wtime() - t;
  60.   cout << nCount << endl << t << endl;
  61.   return 0;
  62. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表