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,先贴上:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. LL B;
  9. int k;
  10. char s[];
  11. int dfs(LL& ans, int &i, LL& c){
  12. int len = strlen(s);
  13. for(i; i < len;){
  14. if(k == )c = ;
  15. if(s[i] == ']'){
  16. i = i + ;
  17. k--;
  18. return s[i - ] - '';
  19. }
  20. else if(s[i] == '['){
  21. LL x = ;
  22. k++;
  23. i++;
  24. int cnt = dfs(x, i, c);
  25. // cout << c << " " << cnt << " " << x << endl;
  26. LL temp = ;
  27. while(cnt--){
  28. ans = ans * c % B + x % B;
  29. temp = temp * c % B;
  30. ans %= B;
  31. }
  32. c = temp;
  33. }
  34. else ans = (ans * + s[i] - '')%B, c = (c * )%B, i++;
  35. }
  36. }
  37. int main(){
  38. int T;
  39. cin >> T;
  40. while(T--){
  41. scanf("%s%lld", s, &B);
  42. int i = ;
  43. LL ans = ;
  44. LL c = ;
  45. k = ;
  46. dfs(ans, i, c);
  47. printf("%lld\n", ans);
  48. }
  49. return ;
  50. }

超内存:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. typedef long long LL;
  7. int dfs(string& ans, string s, int &cur){
  8. for(int i = cur; i < s.length();){
  9. if(s[i] == ']'){
  10. cur = i + ;
  11. return s[i + ] - '';
  12. }
  13. else if(s[i] == '['){
  14. string x;
  15. i++;
  16. int cnt = dfs(x, s, i);
  17. //cout << cnt << endl;
  18. while(cnt--){
  19. ans += x;
  20. }
  21. }
  22. else ans += s[i], i++;
  23. }
  24. }
  25. int main(){
  26. int T;
  27. string s;
  28. LL B;
  29. cin >> T;
  30. while(T--){
  31. cin >> s;
  32. cin >> B;
  33. int i = ;
  34. string A;
  35. dfs(A, s, i);
  36. LL temp = ;
  37. for(int i = ; i < A.length(); i++){
  38. temp = temp * + A[i] - '';
  39. if(temp > B){
  40. temp = temp % B;
  41. }
  42. }
  43. printf("%lld\n", temp);
  44. }
  45. return ;
  46. }

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. 设计: ListView 接口,and the missing read-only interfaces in java collection framework

    Java的集合框架以其成功易用的设计征服了很多人(包括我),并且教科书式的诠释了泛型的应用方式. 我也是被 Joshua Bloch 的书引领入门,从中得益良多.我当然不会认为自己在设计上比他懂得更多 ...

  2. 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)

    题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  3. Class的生命周期

    之前的<JVM类载入机制-ClassLoader>和<初探JVM-ClassLoader源代码>,仅仅是讨论了Class的载入部分,如今来纵观一下整个Class的生命周期. C ...

  4. Android动态加载jar/dex

    前言 在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优 ...

  5. 转载: Javah生成JNI头文件出现找不到类的错误

    错误: 找不到 'com.chnic.jni.SayHellotoCPP' 的类文件. 上图可以看到错误和解决办法. 不要忘记那个点 javah -classpath . -jni com.chnic ...

  6. (转)android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下       首先客户端从服务器端获取json数据 1.利用HttpUrlConnection   复制代码 ...

  7. Oracle查找重复数据

    Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)

  8. SMBUS(IIC)总线

    1.SPI和IIC一般都作为板上通信,UART.SMBUS和USB一般都作为板间通信. 其中SMBUS是参考IIC制定出来的眼生病,两者很像. 2.SMBUS的I/O接口是由两条线组成的双向串行总线. ...

  9. 在Eclipse中运行hadoop程序

    1.下载hadoop-eclipse-plugin-1.2.1.jar,并将之复制到eclipse/plugins下. 2.打开map-reduce视图 在eclipse中,打开window--> ...

  10. 【好程序员笔记分享】——iOS开发之纯代码键盘退出

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之纯代码键盘退出(非常简单)     iOS开发之纯代码键盘退出 前面说到了好几次关于键盘退出的,但 ...