给定一个函数f(n)=(n 1 + n 2 + n 3 + n 4 ),对于任何给定的正整数n值,必须找到f(n)mod 5的值。
注意:n可能足够大,使得f(n)> 10 18 。
例子 :
Input : n = 4
Output : 0
Explanation : f(4) = 4 + 16 + 64 + 256 = 330,
f(4) mod 5 = 330 mod 5 = 0.
Input : n = 1
Output : 4
Explanation : f(1) = 1 + 1 + 1 + 1 = 4,
f(1) mod 5 = 4.
首先,为解决该问题,您可以直接借助任何幂函数和模运算符找到(n 1 + n 2 + n 3 + n 4 )mod 5的值。
但是对于较大的n值,您的结果将是错误的,因为对于较大的n值,f(n)可能超出long long int的范围,在这种情况下,您必须选择其他有效的方法。
为了解决这个问题,让我们对f(n)做一些小的数学推导。
f(n) = (n1 + n2 + n3 + n4)
= (n) (n+1) (n2+1)
Now, for finding f(n) mod 5 we must take care of unit digit of f(n) only,
also as f(n) mod 5 is dependent on n%5, (n+1)%5 & (n2+1)%5,
if any of these three result in zero then our whole result is 0.
So, if n = 5, 10, .. 5k then n mod 5 = 0 hence f(n) mod 5 = 0.
if n = 4, 9, .., (5k-1) then (n+1) mod 5 = 0 hence f(n) mod 5 = 0.
if n = 3, 8, 13..., (5k-2) f(n) mod 5 = (3 * 4 * 10) mod 5 = 0
if n = 2, 7, 12..., (5k-3) f(n) mod 5 = (2 * 3 * 5) mod 5 = 0.
if n = 1, 6, 11..., (5k-4) f(n) mod 5 = (1 * 2 * 2) mod 5 = 4.
经过以上分析,我们可以看到,如果n的形式为5k + 1或5k-4,则f(n)mod 5 = 4,否则f(n)= 0。
IE浏览器if(n%5 == 1)结果= 4,
否则结果= 0。
C++
// finding the value of f(n) mod 5 for given n.
#include
using namespace std;
// function for f(n) mod 5
int fnMod(int n)
{
// if n % 5 == 1 return 4
if (n % 5 == 1)
return 4;
// else return 0
else
return 0;
}
// driver program
int main()
{
int n = 10;
cout << fnMod(n) << endl;
n = 11;
cout << fnMod(n) << endl;
return 0;
}
Java
// Java code to finding the value
// of f(n) mod 5 for given n.
import java.io.*;
class GFG
{
// function for f(n) mod 5
static int fnMod(int n)
{
// if n % 5 == 1 return 4
if (n % 5 == 1)
return 4;
// else return 0
else
return 0;
}
// Driver program
public static void main (String[] args)
{
int n = 10;
System.out.println(fnMod(n));
n = 11;
System.out.println(fnMod(n));
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to find the value
# of f(n) mod 5 for given n.
# Function for f(n) mod 5
def fnMod(n):
# if n % 5 == 1 return 4
if (n % 5 == 1):
return 4
# else return 0
else:
return 0
# Driver Code
n = 10
print(fnMod(n))
n = 11
print(fnMod(n))
# This code is contributed by Smitha Dinesh Semwal
C#
// Code for finding the value
// of f(n) mod 5 for given n.
using System;
class GFG {
// function for f(n) mod 5
static int fnMod(int n)
{
// if n % 5 == 1 return 4
if (n % 5 == 1)
return 4;
// else return 0
else
return 0;
}
// Driver program
public static void Main()
{
int n = 10;
Console.WriteLine(fnMod(n));
n = 11;
Console.WriteLine(fnMod(n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
0
4