The last Digit

Source: Internet
Author: User

Given an Interger N, you is 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 ofS=11+22+.... +nn .

Sample Input and output
sample Input sample Output
3 
2 
1 5 
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] indicates that the last///p[i][j of the POW (i,i) represents the last int main () {////) of the POW (i,10*j+i) {/////Table save data (40 is a large period) for (in T 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;///last saved.            remainder for (int i=0; i<len; i++) {a[i+1] + = ((a[i]%40) * 10);            t = t*10 + a[i]/40;        T%= 5;///t Save how many cycles (40 per cycle, last one and 5 cases)}///The end number of each cycle and is 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;///The number of numbers currently added a[len-1]%= 40;///period except the remainder (remainder) need to be added from the beginning plus 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;}

The last Digit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.