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