10162-last digit (number theory + periodic law)

Source: Internet
Author: User
Ultraviolet A 10162-last digit

Question Link

Question: PleaseS= (11 + 22 +...NN) % 10

Idea: Play 0-9 each cycle, the Discovery cycle is 1 or 2 or 4, so s is 20 cycles, after the table is typed, it is found that 20 is 4, so the corresponding 40 is 8, 60 is 6,100 is 0,100 is 1 cycle, and is 0, so first put the number mod 100, and then find the corresponding position in mod 20.

Code:

#include <stdio.h>#include <string.h>const int Z2[10] = {0, 4, 8, 2 ,6};int T[10][10], tn[10];int Z[25];char str[105];int solve(int mod) {    int len = strlen(str);    int yu = 0;    for (int i = 0; i < len ;i++) {yu = (yu * 10 + str[i] - '0') % mod;    }    return yu;}int main() {    for (int i = 0; i < 10; i++) {int tmp = i;T[i][tn[i]++] = i;tmp = tmp * i % 10;while (tmp != i) {    T[i][tn[i]++] = tmp;    tmp = tmp * i % 10;}    }    for (int i = 1; i <= 20; i++) {int tmp = i % tn[i % 10] - 1;if (tmp < 0) tmp += tn[i % 10];Z[i] = (Z[i - 1] + T[i % 10][tmp]) % 10;        }    /*for (int i = 1; i <= 20; i++)printf("%d\n", Z[i]);    for (int i = 0; i < 10; i++) {printf("%d: zhouqi %d:\n", i, tn[i]);for (int j = 0; j < tn[i]; j++)    printf("%d ", T[i][j]);printf("\n");    }*/    while (~scanf("%s", str) && str[0] != '0') {int yu = solve(100);int ans = (Z[yu % 20] + Z2[yu / 20]) % 10;printf("%d\n", ans);    }    return 0;}


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.