Ultraviolet A 10515-Powers et al. (number theory)

Source: Internet
Author: User

Link: Ultraviolet A 10515-Powers et al.

Give M and N and ask the number of Mn.

Solution: in fact, you only need to look at the last digit of M, judge the period with the most digit, and then use N % T.

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 15;const int maxs = 105;vector<int> g[maxn];char a[maxs], b[maxs];void init () {    int v[maxn];    for (int i = 0; i < maxn; i++) {        int tmp = i;        memset(v, 0, sizeof (v));        while (v[tmp] == 0) {            v[tmp] = 1;            g[i].push_back(tmp);            tmp = tmp * i % 10;        }    }}int main () {    init();    while (scanf("%s%s", a, b) == 2 && ( strcmp(a,"0") || strcmp(b,"0")) ) {        if (strcmp(b, "0") == 0) {            printf("1\n");            continue;        }        int u = a[strlen(a)-1] - ‘0‘;        int len = strlen(b);        int tmp = 1, m = g[u].size();        for (int i = 0; i < len; i++)            tmp = (tmp * 10 + (b[i] - ‘0‘)) % m;        printf("%d\n", g[u][(tmp-1+m)%m]);    }    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.