Codeforces 464c substitutes in number (efficient + fast power)

Source: Internet
Author: User

Link: codeforces 464c substitutes in number

Given a string, and the Transformation Operation in N, convert a number into a string, which may be a null string, and finally convert the string into one

Number, modulo 1 E9 + 7.

Solution: The operation is processed in reverse mode. In this way, the Val and Len values of each number are maintained. Val indicates the modulo 1e9 + 7 for the corresponding value, and Len indicates the number of corresponding values.

Bit.

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e5+5;const ll mod = 1e9+7;char s[maxn], o[maxn];int n;ll v[20], l[20];ll pow_mod (ll x, int n) {    ll ret = 1;    while (n) {        if(n&1)            ret = ret * x % mod;        x = x * x % mod;        n >>= 1;    }    return ret;}struct state {    int r;    vector<int> vec;    void set (char* str) {        int len = strlen(str);        r = str[0] - ‘0‘;        vec.clear();        for (int i = 3; i < len; i++) {            if (str[i] >= ‘0‘ && str[i] <= ‘9‘)                vec.push_back(str[i] - ‘0‘);        }    }    void solve () {        ll val = 0, len = 0;        for (int i = 0; i < vec.size(); i++) {            int u = vec[i];            len += l[u];            val = (val * pow_mod(10, l[u]) + v[u]) % mod;            len %= (mod-1);        }        v[r] = val;        l[r] = len;    }}com[maxn];void init () {    for (int i = 0; i < 10; i++) {        v[i] = i;        l[i] = 1;    }    for (int i = 0; i < n; i++) {        scanf("%s", o);        com[i].set(o);    }    for (int i = n - 1; i >= 0; i--) {        int u = com[i].r;        com[i].solve();    }}int main () {    scanf("%s%d", s, &n);    init();    int len = strlen(s);    ll ans = 0;    for (int i = 0; i < len; i++) {        int u = s[i] - ‘0‘;        ans = (ans * pow_mod(10, l[u]) + v[u]) % mod;    }    printf("%lld\n", ans);    return 0;}

Codeforces 464c substitutes in number (efficient + fast power)

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.