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 Input Sample 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;
}
时间: 2024-08-29 16:32:59

The Last Digit的相关文章

LeetCode 233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个

TOJ 2703: Cow Digit Game

2703: Cow Digit Game Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 1            Accepted:1 Description Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory. Game i start

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

&lt;hdu - 1600 - 1601&gt; Leftmost Digit &amp;&amp; Rightmost Digit 数学方法求取大位数单位数字

1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的: 两边取log10有:lg(n^n) = lg(a * 10^x); 即:n * lg(n)  - x = lg(a); 现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数: 所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的

HDU 1061 Rightmost Digit

Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test ca

Arrange an Array to Form a Smallest Digit

/** * Input an array of positive integers, arrange the integers to form new digits, * and output the smallest digit among all the new ones. * Input Example 1: * {2, 1} * Output Example 1: * 12 * * Input Example 2: * {32, 321} * Output Example 2: * 32

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

[HDOJ]Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38642    Accepted Submission(s): 14558 Problem Description Given a positive integer N, you should output the most right digit of N

HDU_1061:Rightmost Digit

Problem Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.Each

hdu 1066 Last non-zero Digit in N! (数论——n!中的最后一个非0数字)

Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6432    Accepted Submission(s): 1593 Problem Description The expression N!, read as "N factorial," denotes the pro