📌  相关文章
📜  从1到N的所有整数的最后一位数的总和,可被M整除

📅  最后修改于: 2021-04-21 21:33:29             🧑  作者: Mango

给定两个整数NN。任务是计算可以从M整除的从1N的所有整数的最后一位数字的和。

例子:

方法:问题基于简单的观察。

令k = floor(N / M)是从1到N的整数(可被M整除)的数目。

由于需要将最后的数字添加到原始总和中。因此,最后一位可以在0到9的范围内,并且可以通过观察阵列模式来形成周期。

因此,对于每个循环,sum =前10个最后一位的总和,之后,我们可以将k除以10,然后从开始算起剩余数字的最后一位。

因此, Sum = (循环数*可被M整除的前10个整数的最后一位数字的总和)+(可被M整除的k%10整数的最后一位数字的总和)。

下面是上述方法的实现:

C++
// C++ implementation
// of the approach
#include
using namespace std;
  
#define long long long
  
// Function to return the 
// required sum
long sumOfLastDig(long n, long m) {
  
    long sum = 0, k;
  
    // Number of element beetween
    // 1 to n divisible by m
    k = n/m;
  
    // Array to store the last digit
    // of elements in a cycle
    long arr[10];
  
    // Storing and adding last
    // digit of cycle
    for (int i = 0; i < 10; i++) {
        arr[i] = m*(i+1) % 10;
        sum += arr[i];
    }
  
    // Number of elements
    // present in last cycle
    long rem = k % 10;
  
    // Sum of k/10 cycle
    long ans = (k/10)*sum;
  
    // Adding value of digits
    // of last cycle to the answer
    for (int i = 0; i < rem; i++) {
        ans += arr[i];
    }
  
    return ans;
}
  
// Driver Code
int main() {
  
    // input n and m
    long n = 100, m = 3;
  
    cout<


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the required sum
static long sumOfLastDig(long n, long m) 
{
    long sum = 0, k;
  
    // Number of element beetween
    // 1 to n divisible by m
    k = n / m;
  
    // Array to store the last digit
    // of elements in a cycle
    long []arr = new long[10];
  
    // Storing and adding last
    // digit of cycle
    for (int i = 0; i < 10; i++) 
    {
        arr[i] = m * (i + 1) % 10;
        sum += arr[i];
    }
  
    // Number of elements
    // present in last cycle
    long rem = k % 10;
  
    // Sum of k/10 cycle
    long ans = (k / 10) * sum;
  
    // Adding value of digits
    // of last cycle to the answer
    for (int i = 0; i < rem; i++) 
    {
        ans += arr[i];
    }
    return ans;
}
  
// Driver Code
public static void main (String[] args)
{
  
    // input n and m
    long n = 100, m = 3;
      
    System.out.println(sumOfLastDig(n, m));
}
}
  
// This code is contributed by jit_t


Python3
# Python3 implementation of the approach 
  
# Function to return the 
# required sum 
def sumOfLastDig(n, m) : 
  
    sum = 0; 
  
    # Number of element beetween 
    # 1 to n divisible by m 
    k = n // m;
      
    # Array to store the last digit
    # of elements in a cycle
    arr = [0] * 10; 
  
    # Storing and adding last 
    # digit of cycle 
    for i in range(10) :
        arr[i] = m * (i + 1) % 10; 
        sum += arr[i]; 
  
    # Number of elements 
    # present in last cycle 
    rem = k % 10; 
  
    # Sum of k/10 cycle 
    ans = (k // 10) * sum; 
  
    # Adding value of digits 
    # of last cycle to the answer 
    for i in range(rem) :
        ans += arr[i]; 
  
    return ans; 
  
# Driver Code 
if __name__ == "__main__" : 
  
    # input n and m 
    n = 100; m = 3; 
  
    print(sumOfLastDig(n, m)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the required sum
static long sumOfLastDig(long n, long m) 
{
    long sum = 0, k;
  
    // Number of element beetween
    // 1 to n divisible by m
    k = n / m;
  
    // Array to store the last digit
    // of elements in a cycle
    long []arr = new long[10];
  
    // Storing and adding last
    // digit of cycle
    for (int i = 0; i < 10; i++) 
    {
        arr[i] = m * (i + 1) % 10;
        sum += arr[i];
    }
  
    // Number of elements
    // present in last cycle
    long rem = k % 10;
  
    // Sum of k/10 cycle
    long ans = (k / 10) * sum;
  
    // Adding value of digits
    // of last cycle to the answer
    for (int i = 0; i < rem; i++) 
    {
        ans += arr[i];
    }
    return ans;
}
  
// Driver Code
static public void Main ()
{
      
    // input n and m
    long n = 100, m = 3;
      
    Console.Write(sumOfLastDig(n, m));
}
}
  
// This code is contributed by ajit.


输出:
153