Fzu2108:mod Problem (mathematics)

Source: Internet
Author: User

Problem Description

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

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

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

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

(2) If W is valid and then [w]x if valid. Here's 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 was valid (which is 2312311).

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

Input

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

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

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

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

Assume that is the length of number A in decimal notation would less than 2^63.

Outputfor each test case, output A Mod B. Sample Input3[0]9[[1]2]310007[[213231414343214231]5]110007[0012]11 Sample Output103439430


The problem is the understanding of modulo operations.

For example [123]2=123123=123*1000+123. So if b=11, what we're going to calculate is:

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

Go ahead and simplify, remember a = 123% 11,m = 1000 11.

Ans = (A * m% one + a)% 11.

For this m, we can calculate its value with a fast power, while the rest of the rest is simply a matter of decomposition and accumulation, which are not difficult.

Math puzzles solved, the rest is to use deep search to restore this data from the string, this is the problem of coding.



#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define LL __ Int64struct node{ll Val,len;};    Char Str[1005];int t,mod;ll qmod (ll A,ll b) {ll s = 1;        while (b) {if (b&1) s = (s*a)%mod;        A = (a*a)%mod;    b>>=1; } return s%mod;}    Node DFS (int l,int r) {int i,j,top = 0;    int left,right,k,cnt;    Node ans;    Ans.val = 0;    Ans.len = 0;            for (i = l; i<=r; i++) {if (str[i]== ' [') {if (!top) left=i+1;        ++top;            } else if (str[i]== '] ') {--top;                if (!top) {right = I-1;                i++;                CNT = str[i]-' 0 ';                Node Tem=dfs (left,right);                ll base = Qmod (10,tem.len);                Ans.len+=cnt*tem.len;                Ans.val= (Ans.val*qmod (base,cnt))%mod;                ll S=1,bb=base; while (--cnt) {s+=BB;                    bb= (bb*base)%mod;                S%=mod;                } tem.val= (tem.val*s)%mod;            Ans.val = (ans.val+tem.val)%mod;            }} else if (!top) {ans.val=ans.val*10+str[i]-' 0 ';            Ans.val%=mod;        ans.len++; }} return ans;    int main () {scanf ("%d", &t);        while (t--) {scanf ("%s%d", str,&mod);        Node ans = DFS (0,strlen (str)-1);    printf ("%i64d\n", ans.val); } return 0;}


Fzu2108:mod Problem (mathematics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.