[HDU 4933] miaomiao's function digital DP + large number

Source: Internet
Author: User
Miaomiao's Function Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 79 accepted submission (s): 18


Problem descriptionfirstly, miaomiao define two functions F (x), g (x ):


(K is the smallest integer which satisfied x + 9 * k> 0)

If e.g. G (178) = 1-7 + 8 = 2, G (1) = 1, g (1234) = 1-2 + 3-4 =-2;

For example F (20140810) = F (2 + 0 + 1 + 4 + 0 + 8 + 1 + 0) = f (16) = f (1 + 6) = f (7) = 7

Then, you are given two integers L, R (L <= r ).
Answer is defined.
Please caculate (answer mod f (answer) + f (answer) Mod f (answer ).
Pay attantion! If F (answer) = 0, please output "error! "
Inputthere are using test cases. In the first line there is a number T (t <= 50) shows the number of test cases.
For each test cases there are two numbers L, R (0 <= L, r <= 10 ^ 100 ).

For your information, l, r don't have leading zeros.
Outputfor each opeartion, output the result.
Sample Input
20 00 21
 
Sample output
Error!1HintThe Answer is 0 and 13.So the result is Error! and 13 Mod (1 + 3) = 1 
 

Source: bestcoder #4 C

Theme

Returns the interlaced and sum of all numbers in [L, R], which is defined

Given a number X, it is set to A0 in sequence from the decimal position to the lower position ,? A1 ,?...,? An? -? 1. Define the interlaced and function:

F (x )? =? A0? -? A1? +? A2? -?...? +? (? -? 1) n? -? 1An? -? 1

It's actually the question of hihocoder 1033.

And perform a modulo processing for ans.


Solutions

The method for this question is exactly the same as that for hihocoder 1033. If no formula is provided, the memory DFS + Java large numbers are written ......

Note that there may be two final results: 0 and 9. Therefore, an error of the modulus of the answer will be changed to 0 if the modulo is directly set to 9.

Special Judgment


Code:

import java.util.*;import java.math.*;public class Main{static class node{public BigInteger s=BigInteger.ZERO,n=BigInteger.ZERO;}public static int top=0,bn=0;public static node[] dp=new node[105];public static int[] bt=new int[105];public static node dfs(int pos,int limit){node tmp = new node();tmp.s=BigInteger.ZERO;tmp.n=BigInteger.ZERO;if (pos==0) {tmp.s=BigInteger.ZERO;tmp.n=BigInteger.ONE;return tmp;}if (limit==0&&(!dp[pos].n.equals(BigInteger.ZERO))) return dp[pos];int head=(pos==top)?1:0;int tail=(limit==1)?bt[pos]:9;for (int i=head;i<=tail;i++){node s= dfs(pos-1,(limit==1&&bt[pos]==i)?1:0);tmp.n=tmp.n.add(s.n);tmp.s=tmp.s.add(s.s);if ((top-pos)%2==0)tmp.s=tmp.s.add((s.n).multiply(BigInteger.valueOf(i)));else tmp.s=tmp.s.add((s.n).multiply(BigInteger.valueOf(-i)));}if (limit==0) dp[pos]=tmp;return tmp;}public static BigInteger f(BigInteger x){BigInteger ans=BigInteger.ZERO;if (x.equals(BigInteger.ZERO)) return ans;if (x.equals(BigInteger.valueOf(-1))) return ans;bn=0;while (!x.equals(BigInteger.ZERO)){bn++;bt[bn]=x.mod(BigInteger.valueOf(10)).intValue();x=x.divide(BigInteger.valueOf(10));}for (top=1;top<=bn;top++){for (int i=1;i<=104;i++){dp[i]=new node();dp[i].s=BigInteger.ZERO;dp[i].n=BigInteger.ZERO;}ans=ans.add(dfs(top,(top==bn)?1:0).s);}return ans;}public static BigInteger g(BigInteger x){BigInteger t=BigInteger.ZERO;BigInteger p=x;if (x.equals(BigInteger.ZERO)) return x;if (x.compareTo(BigInteger.valueOf(0))<0)if (x.mod(BigInteger.valueOf(9)).equals((BigInteger.valueOf(0)))) return(BigInteger.valueOf(9));else return  (g(x.multiply(BigInteger.valueOf(-8))));if (x.mod(BigInteger.valueOf(9)).equals(BigInteger.ZERO))return BigInteger.valueOf(9);else return x.mod(BigInteger.valueOf(9));}public static void main(String [] args){Scanner in=new Scanner(System.in);int T=in.nextInt();while (T--!=0){BigInteger L=in.nextBigInteger();BigInteger R=in.nextBigInteger();BigInteger ans=f(R).add(f(L.add((BigInteger.valueOf(-1)))).negate());BigInteger t=g(ans);if (t.equals(BigInteger.valueOf(0)))System.out.println("Error!");else System.out.println((ans.mod(t).add(t)).mod(t));}}} 



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.