📌  相关文章
📜  给定n的级数(1 ^ 3 + 2 ^ 3 + 3 ^ 3 +…+ n ^ 3)mod 4

📅  最后修改于: 2021-04-24 03:32:41             🧑  作者: Mango

给定函数f(n)=(1 3 + 2 3 + 3 3 +…+ n 3 ),任务是找到给定正整数’n’的f(n)mod 4值。
例子

Input: n=6
Output: 1
Explanation: f(6) = 1+8+27+64+125+216=441
f(n) mod 4=441 mod 4 = 1

Input: n=4
Output: 0
Explanation: f(4)=1+8+27+64 = 100
f(n) mod 4 =100 mod 4 = 0

解决此问题时,您可以直接计算(1 3 + 2 3 + 3 3 +…+ n 3 )mod4。但这需要O(n)时间。我们可以将直接公式用于多维数据集的总和,但是对于较大的“ n”,我们可能会使f(n)超出long long int的范围。
这是一个有效的O(1)解决方案:
从除法算法中我们知道,每个整数都可以表示为4k,4k + 1、4k + 2或4k + 3。
现在,

现在,让x为最大整数,该整数不大于n并被4整除。因此,我们可以很容易地看到,
(1 3 +2 3 +3 3 ….. + x 3 )mod 4 = 0。
现在,

  • 如果n是4的除数,则x = n并且f(n)mod 4 = 0。
  • 否则,如果n的形式为4k +1,则x = n-1。因此,f(n)= 1 3 + 2 3 + 3 3 ….. + x 3 + n 3 )mod 4 = n ^ 3 mod 4 = 1
  • 类似地,如果n的形式为4k + 2(即x = n-2),我们可以很容易地证明f(n)=((n-1) 3 + n 3 )mod 4 =(1 + 0)mod 4 = 1
  • 如果n的形式为4k + 3(x = n-3)。类似地,我们得到f(n)mod 4 =((n-2) 3 +(n-1) 3 + n 3 )mod 4 =(1 + 0 + 3)mod 4 = 0

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// function for obtaining the value of f(n) mod 4
int fnMod(int n)
{
    // Find the remainder of n when divided by 4
    int rem = n % 4;
 
    // If n is of the form 4k or 4k+3
    if (rem == 0 || rem == 3)
        return 0;
 
    // If n is of the form 4k+1 or 4k+2
    else if (rem == 1 || rem == 2)
        return 1;
}
 
// Driver code
int main()
{
    int n = 6;
    cout << fnMod(n);
    return 0;
}


Java
// Java implementation of the approach
 
import java .io.*;
 
class GFG {
  
// function for obtaining the value of f(n) mod 4
static int fnMod(int n)
{
    // Find the remainder of n when divided by 4
    int rem = n % 4;
 
    // If n is of the form 4k or 4k+3
    if (rem == 0 || rem == 3)
        return 0;
 
    // If n is of the form 4k+1 or 4k+2
    else if (rem == 1 || rem == 2)
        return 1;
        return 0;
}
 
// Driver code
 
    public static void main (String[] args) {
            int n = 6;
    System.out.print( fnMod(n));
    }
}
//This code is contributed
// by shs


Python 3
# Python3 implementation of
# above approach
 
# function for obtaining the
# value of f(n) mod 4
def fnMod(n) :
 
    # Find the remainder of n
    # when divided by 4
    rem = n % 4
 
    # If n is of the form 4k or 4k+3
    if (rem == 0 or rem == 3) :
        return 0
 
    # If n is of the form
    # 4k+1 or 4k+2
    elif (rem == 1 or rem == 2) :
        return 1
 
# Driver code    
if __name__ == "__main__" :
 
    n = 6
    print(fnMod(n))
 
# This code is contributed
# by ANKITRAI1


C#
// C# implementation of the approach
  
 
using System;
class GFG {
   
// function for obtaining the value of f(n) mod 4
static int fnMod(int n)
{
    // Find the remainder of n when divided by 4
    int rem = n % 4;
  
    // If n is of the form 4k or 4k+3
    if (rem == 0 || rem == 3)
        return 0;
  
    // If n is of the form 4k+1 or 4k+2
    else if (rem == 1 || rem == 2)
        return 1;
        return 0;
}
  
// Driver code
  
    public static void Main () {
            int n = 6;
    Console.Write( fnMod(n));
    }
}


PHP


Javascript


输出:
1