简体   繁体   English

给定数字N,多少对数字的平方和小于或等于N?

[英]Given a number N how many pairs of numbers have square sum less than or equal to N?

Let's define F(N) as the number of pairs of distinct positive integers (A,B) such that A 2 +B 2 ≤N and A<B . 让我们定义F(N),其对不同的正整数(A,B),使得A 2 + B 2≤NA <B的数。

If N=5 the only possible such pair is (1,2) for N=10 the pairs are two: (1,2) and (1,3) . 如果N = 5,则唯一可能的配对是(1,2) ,而N = 10,则配对是两个: (1,2)(1,3)

Furthermore we have F(13)=3 , F(17)=4 , F(17)=4 , F(20)=5 , F(20)=5 , F(25)=6 , F(100)=31 and so on for every number which is sum of two distinct non-zero squares. 此外,我们有F(13)= 3F(17)= 4F(17)= 4F(20)= 5F(20)= 5F(25)= 6F(100)=每个数字都是31 ,依此类推,这是两个不同的非零平方之和。

So far I have the following solution: 到目前为止,我有以下解决方案:

long long SOLVE(lld n)
{
    long long x=sqrt(n),up=0;
    long long a=x,b=1;
    while(abs(a-(b-1))!=1)
    {

        while(sqr(a)+sqr(b)<=n )
        {
            b++;
        }
        up+=(b-1);
        a--;
    }
    b--;
    up+=(b*(b+1))/2;
    return up;
}
int main()
{
      cout<<number(100);
return 0;
}

Same numbers are not countable, thus (1,1) and (2,2) are invalid tuples. 相同的数字不可计数,因此(1,1)(2,2)是无效的元组。 Same combination but different order counts only once. 相同的组合但不同的订单仅计算一次。 Thus (1,2) and (2,1) count only as once. 因此(1,2)(2,1)仅计一次。

But as the range of N is 1, I need a more efficient algorithm or formula to calculate this. 但是,由于N的范围为1,因此我需要一种更有效的算法或公式来进行计算。 Is there any trick to make my code more efficient? 有什么技巧可以使我的代码更高效?

In pseudocode: 用伪代码:

int count=0;
for (smaller=1; ;++smaller)
{
    maxlarger = floor(sqrt(N-smaller*smaller));
    if (maxlarger <= smaller)
        break;
    count+=(maxlarger-smaller);
}
return count;

You do not have to calculate the number of B 's: you can simply calculate the largest B for which this is possible, which is immediately the number of tuples for that A : 您不必计算B的数目:您可以简单地计算出可能的最大B数,即立即为该A的元组数:

B max =sqrt(NA 2 ) , and the lower-bound on B is: B min =A+1 . B max = sqrt(NA 2 ,并且B的下限是: B min = A + 1

Now you can do the following: 现在,您可以执行以下操作:

  • iterate for A from 1 to sqrt(n) ; A1迭代到sqrt(n) ;
  • for each such A calculate the amount of B 's you can use; 为每个这样的A计算您可以使用的B的数量;
  • calculate the sum of these. 计算这些总和。

So this leads us to the following algorithm: 因此,这导致我们得出以下算法:

lld SOLVE(lld n) {
    lld aM=sqrt(n);
    lld a=1;
    lld res = 0;
    for(lld a = 1; a < aM; a++) {
        int nB = sqrt(n-a*a)-a;
        if(nB > 0) {
            res += nB;
        } else {
            break;
        }
    }
    return res;
}

from the moment, no more B values can be found, one can break off the search. 从那一刻起,再也找不到B值,可以中断搜索。

I've written a demo here which seems to work. 我在这里编写了一个似乎可行的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 小于或等于 n 的所有丰富数的总和 - sum of all abundant numbers less than or equal to n 给定一个N个数的序列,提取长度K范围小于R?的序列数。 - Given a sequence of N numbers ,extract number of sequences of length K having range less than R? 位数之和等于给定数 n 的最小数 - Smallest number whose sum of the digits is equal to the given number n 从小于或等于某个 N 的数字列表中最小化或找到理想的子集和 - Minimize or find a n ideal subset-sums from a list of numbers which are less than or equal to some N 给定数字 n 找到它之前的偶数正数的总和 - Given number n find the sum of the even positive numbers before it 给定数字n,则打印系列中的第一个大于或等于n的数字 - Given a number n, print the first number of the series greater than or equal to n 数组中有多少个数小于或等于 x? - How many number are less than or equal than x in an array? 如何产生随机数,使它们的和等于给定数? - How to produce random numbers so that their sum is equal to given number? 如何找到给定数N且时间复杂度小于N的所有因子? - how to find all factors of a given number N with time complexity less then N? 给定 N 个范围。 有多少种方法可以从这些总和为某个值的范围中取出 N 个数字? - Given N ranges. How many ways are there to take N numbers from these ranges that sums up to a certain value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM