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\n", ans);
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/PersistFaith/p/4930126.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值