Codeforces 464C Substitutes in Number 同餘定理+類比,codeforces464c

來源:互聯網
上載者:User

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*/





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.