Codeforces 464C Substitutes in Number(高效+快速幂)
来源:互联网 发布:金融风险管理知乎 编辑:程序博客网 时间:2024/05/21 13:53
题目链接:Codeforces 464C Substitutes in Number
题目大意:给定一个字符串,以及n中变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一
个数,取模1e9+7。
解题思路:将操作倒过来处理,这样维护每个数来的val,len两个,val表示对应数值取模1e9+7,len表示对应有多少
位,再计算的过程中要使用。
#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;}
0 0
- Codeforces 464C Substitutes in Number(高效+快速幂)
- codeforces 464C Substitutes in Number
- Codeforces 464C Substitutes in Number
- 【DP】 codeforces 464C Substitutes in Number
- Codeforces 464 C. Substitutes in Number
- codeforces 464C Substitutes in Number dp
- codeforces #464C Substitutes in Number
- Codeforces 464C Substitutes in Number 同余定理+模拟
- Codeforces 464 C. Substitutes in Number 动态规划法题解
- Codeforces Round #265 (Div. 2)E. Substitutes in Number(数学)
- Codeforces Round #265 (Div. 2) E. Substitutes in Number
- Codeforces 466C Number of Ways(高效)
- Codeforces 359C Prime Number (数学+快速幂)
- [矩阵快速幂] CodeForces 392C Yet Another Number Sequence
- CodeForces 359C Prime Number 数学+快速幂
- Codeforces 392-C Yet Another Number Sequence (矩阵快速幂)
- Codeforces 392C Yet Another Number Sequence(矩阵快速幂)
- CodeForces 392 C.Yet Another Number Sequence(矩阵快速幂)
- Java(二)--OOP设计思想
- [ZOJ 3812 We Need Medicine] DP
- Leetcode dfs Letter Combinations of a Phone Number
- 机房收费系统之添加和删除用户
- Python Selenium 问题记录
- Codeforces 464C Substitutes in Number(高效+快速幂)
- Codeforces Round #265 (Div. 2)
- Gogent使用指南
- 抛硬币问题(美团笔试题)
- 【索引】Codeforces Round #265 (Div. 2)
- KMP算法的next[]数组通俗解释
- 黑马程序员-泛型集合的综合案例(附通配符注意事项)
- CVE 2013-3897 - UAF 漏洞分析学习学习
- UVa869 - Airline Comparison(并查集)