The Last Digit

Given an interger N, you are supposed to tell me the last digit of S=11+22+....+NN.

Input

The first line contains an integer N (1 <= N <= 10^(1e5))

Output

Output a digit meaning the last digit of S=11+22+....+NN.

Sample input and output

Sample InputSample Output
3
2
15
8
#include<stdio.h>
#include<string.h>
#include<math.h>
typedef long long LL;
const int MAXN = 1e5+7;
char s[MAXN];
LL p[10][6], a[MAXN];
///p[i][0]表示pow(i,i)的最后一位
///p[i][j]表示pow(i,10*j+i)的最后一位
int main()
{
    ///先打表保存数据(40为一个大周期)
    for(int i=1; i<10; i++)
    {
        p[i][0] = (LL)(pow(i, i)+0.1) % 10;
        for(int j=1; j<4; j++)
        {
            p[i][j] = p[i][j-1] * LL(pow(i, 10)+0.1);
            p[i][j] %= 10;
        }
    }

    while(scanf("%s", s) != EOF)
    {
        int len = strlen(s), ans=0, t=0;

        for(int i=0; i<len; i++)
            a[i] = s[i]-'0';
        a[len] = 0;///最后一位保存。。余数
        for(int i=0; i<len; i++)
        {
            a[i+1] += ((a[i]%40) * 10);
            t = t*10 + a[i]/40;

            t %= 5;///t保存有多少个周期(每个周期40,最后一位的和有5种情况)
        }
        ///每个周期的末尾数字和是8
        if(t == 1)
            ans = 8;
        else if(t == 2)
            ans = 6;
        else if(t == 3)
            ans = 4;
        else if(t == 4)
            ans = 2;

        int k = 0;///当前已经加上的数的个数
        a[len-1] %= 40;///周期除完剩下的数(余数)需要从头再加上
        for(int i=0; i<4; i++)
        for(int j=0; j<=9; j++)
        {
            if(i==0 && j==0)
                continue;
            if(k == a[len-1])
                break;
            else
            {
                ans += p[j][i];
                ans %= 10;
                k++;
            }
        }

        printf("%d
", ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/PersistFaith/p/4930126.html