K

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Given one non-negative integer A and one positive integer B, it’s very easy for us to calculate A Mod B. Here A Mod B means the remainder of the answer after A is divided by B. For example, 7 Mod 5 = 2, 12 Mod 3 = 0, 0 Mod 3 = 0.

In this problem, we use the following rules to express A.

(1) One non-empty string that only contains {0,1,2,3,4,5,6,7,8,9} is valid.

For example, 123, 000213, 99213. (Leading zeros is OK in this problem)

(2) If w is valid, then [w]x if valid. Here x is one integer that 0<x<10.

For example, [012]2=012012, [35]3[7]1=3535357.

(3) If w and v are valid, then wv is valid.

For example, w=[231]2 and v=1, then wv=[231]21 is valid (which is 2312311).

Now you are given A and B. Here A is express as the rules above and B is simply one integer, you are expected to output the A Mod B.

Input

The first line of the input contains an integer T(T≤10), indicating the number of test cases.

Then T cases, for any case, only two lines.

The first line is one non-empty and valid string that expresses A, the length of the string is no more than 1,000.

The second line is one integer B(0<B<2,000,000,000).

You may assume that the length of number A in decimal notation will less than 2^63.

Output

For each test case, output A Mod B in a single line.

Sample Input

3
[0]9[[1]2]3
10007
[[213231414343214231]5]1
10007
[0012]1
1

Sample Output

1034 3943 0
题解:【】后面代表里面的数字出现的次数;直接暴力会超内存

比如说[123]2=123123=123*1000+123。那么如果B=11,我们所要计算的就是:

[123]2 % 11 = 123123 % 11 = 123*1000 % 11 + 123 % 11.

接着化简下去,记a = 123 % 11,m = 1000 % 11.

ans = (a * m % 11 + a) % 11.

改了下,数据对上了,好像还是wa,先贴上:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL B;
int k;
char s[];
int dfs(LL& ans, int &i, LL& c){
int len = strlen(s);
for(i; i < len;){
if(k == )c = ;
if(s[i] == ']'){
i = i + ;
k--;
return s[i - ] - '';
}
else if(s[i] == '['){
LL x = ;
k++;
i++;
int cnt = dfs(x, i, c);
// cout << c << " " << cnt << " " << x << endl;
LL temp = ;
while(cnt--){
ans = ans * c % B + x % B;
temp = temp * c % B;
ans %= B;
}
c = temp;
}
else ans = (ans * + s[i] - '')%B, c = (c * )%B, i++;
}
}
int main(){
int T;
cin >> T;
while(T--){
scanf("%s%lld", s, &B);
int i = ;
LL ans = ;
LL c = ;
k = ;
dfs(ans, i, c);
printf("%lld\n", ans);
}
return ;
}

超内存:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int dfs(string& ans, string s, int &cur){
for(int i = cur; i < s.length();){
if(s[i] == ']'){
cur = i + ;
return s[i + ] - '';
}
else if(s[i] == '['){
string x;
i++;
int cnt = dfs(x, s, i);
//cout << cnt << endl;
while(cnt--){
ans += x;
}
}
else ans += s[i], i++;
}
}
int main(){
int T;
string s;
LL B;
cin >> T;
while(T--){
cin >> s;
cin >> B;
int i = ;
string A;
dfs(A, s, i);
LL temp = ;
for(int i = ; i < A.length(); i++){
temp = temp * + A[i] - '';
if(temp > B){
temp = temp % B;
}
}
printf("%lld\n", temp);
}
return ;
}

FZU 2108(dfs模拟,大数取余)的更多相关文章

  1. 2019计蒜之道初赛3 D. 阿里巴巴协助征战SARS(困难)(大数取余+欧拉降幂)

    阿里巴巴协助征战SARS(困难) 33.29% 1000ms 262144K   目前,SARS 病毒的研究在世界范围内进行,经科学家研究发现,该病毒及其变种的 DNA 的一条单链中,胞嘧啶.腺嘧啶均 ...

  2. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

  3. POJ2635The Embarrassed Cryptographer(大数取余+素数筛选+好题)

    题目链接 题意:K是由两个素数乘积,如果最小的素数小于L,输出BAD最小的素数,否则输出GOOD 分析 素数打表将 L 大点的素数打出来,一定要比L大,然后就开始枚举,只需K对 素数 取余 看看是否为 ...

  4. java大数取余

    java大数取余: 类方法:BigInteger.divideAndRemainder() 返回一个数组,key = 0为商key = 1为余数 import java.util.*; import ...

  5. hdu 1226 bfs+余数判重+大数取余

    题目: 超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)

    题目链接 C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...

  7. HDU - 4704 sum 大数取余+欧拉降幂

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  8. 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)

    Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...

  9. hdu1226 超级密码 (BFS,里面用了大数取余原理)

    Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进 ...

随机推荐

  1. Android软键盘弹出时布局问题

    最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...

  2. Java 下各种设计模式小结

    策略模式--定义算法族,分别封装起来,让它们之间能够互相替换,此模式让算法的变化独立于使用算法的客户.     策略模式是说,针对一种计算,定义一系列的算法,由用户决定详细使用哪一个算法完毕计算. 比 ...

  3. [Javascript] Call Stack

    Every time when a function run it will be push into the call stack and put on the top,  you can thin ...

  4. Java_.jar .war .ear 详解

      .jar 全称:                     java archive: 包含:                     class.properties文件,是文件封装的最小单元: ...

  5. 网络测试工具netperf

    Netperf是一种网络性能的测量工具,主要针对基于TCP或UDP的传输.Netperf根据应用的不同,可以进行不同模式的网络性能测试,即批量数据传输(bulk data transfer)模式和请求 ...

  6. 破解Veeam过程

    1)运行Veeam_Backup_Setup.exe,但是不要继续下一步: 2)进入到%temp%\IXP000.TMP目录,例如windows xp sp3环境默认为C:\Documents and ...

  7. Jquery:Jquery中的事件<二>

    这几天快忙死了,办了离职还得办入职,完全打乱了我的计划,但是能有一个理想的工作,还是很开心的,以后加把劲,争取把计划再赶上来!不说了,学习!!! 五.事件对象的属性 1.event.type:获取事件 ...

  8. oracle 自动添加序号列 排序

    select      HSL.sortno,                    HSL.B,                    HSL.A,                    row_n ...

  9. Codeforces round #353div2 C

    题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/C 题目大意:给你n个数字,代表这个人在n个银行里面 ...

  10. DOM之节点|属性

    1.查询文档的一个或多个元素有如下方法 a. 用指定的id属性:(若一个文档中有两个相同的id,只会选择第一个;在低于IE8的IE中,getElementById()对匹配元素的ID不区分大小写,而且 ...