Codeforces 464C Substitutes in Number 同餘定理+類比,codeforces464c
題目連結:點擊開啟連結
題意:
給定一串數字
下面有n個操作
每行格式形如 d->t
d為一位元字,t為任意長度的數字。
t的長度和不超過100000
問:最後的結果%1e9+7
思路:
首先我們可以得到一個結論:
同餘定理使用後不能再修改數字。
那麼為了讓同餘定理能夠使用,我們倒序處理每個數字,這樣就能保證能夠使用同餘定理。
記錄每個數字實際代表的數字和實際對應的位元。
然後倒序處理上來即可。
#include <stdio.h>#include <string.h>#include <iostream>#include <math.h>#include <queue>#include <set>#include <algorithm>using namespace std;#define N 100005#define M 100005typedef long long ll;const ll mod = 1000000007;char s[N], t[N];struct node{int d;string s;}a[N];ll v[10], wei[10], d[M];ll W(ll x){if(x==0)return 1LL;ll ans = 0;while(x){x/=10; ans++;}return ans;}int n;void solve(){scanf("%d",&n); getchar();for(int i = 1; i <= n; i++) {scanf("%d->", &a[i].d);gets(t);a[i].s = t;}for(int i = 0; i < 10; i++) {v[i] = i , wei[i] = 10LL;}for(int i = n; i >= 1; i--){ll now = 0;ll len = (ll)a[i].s.length();if(len == 0) { v[ a[i].d ] = 0; wei[ a[i].d ] = 1; continue;}ll X = 1;for(ll j = 0; j < len; j++){now *= wei[a[i].s[j]-'0'];now += v[ a[i].s[j]-'0'];X = (X * wei[ a[i].s[j]-'0' ])%mod;now %= mod;}v[ a[i].d ] = now;wei[a[i].d] = X;}ll ans = 0;for(int i = 0; s[i]; i++) {ans *= wei[ s[i]-'0' ];ans += v[ s[i]-'0' ];ans %= mod;}cout<<ans<<endl;}int main(){while(gets(s))solve();return 0;}/*12312312->0012312313->22222->00->710000000080*/