HDU 2266 How many equations Can you Find (DFS)

Source: Internet
Author: User

How many equations Can you Find

Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u

Description

Now give you a string which only contains 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.You is asked to add the sign ' + ' or '-' between t He characters. Just like give your a string "12345", you can work out a string "123+4-5". Now give you a integer N, please tell me what many ways can you find and make the result of the string equal to N. You can only be choose at the most one sign between the adjacent characters.

Input

Each case contains a string s and a number N. You could be sure the length of the string would not exceed and the absolute value of N would not exceed 999999999999.

Output

The output contains one line for each data set:the number of ways can find and make the equation.

Sample Input

123456789 3 21 1

Sample Output

18 1

Simple translation:

Give you two number n,m. You need to fill in the middle of N with a plus or minus sign, or leave it blank. Let the value of this part of n be equal to M. Ask how many kinds of programs.

An example: n=12345.

You can manipulate it so that it becomes so 123+4-5=122.

Problem Solving Ideas:

DFS, considering the number of numbers already processed and the current value of this part, is processed to the end point, if equal to M, result +1.

Code:

#include <cstdio>#include<cstring>#defineLL Long Longusing namespacestd;Charstring[ -]; LL equation;intLength,answer;voidSearch_for_answer (intnow_position,ll Now_value) {    if(now_position>=Length) {        if(now_value==equation) answer++; return; } LL Temp_value=0;  for(intI=1; i<=length-now_position;i++) {Temp_value=temp_value*Ten+string[i-1+now_position]-'0'; Search_for_answer (Now_position+i,now_value+temp_value); if(now_position) search_for_answer (now_position+i,now_value-temp_value); }}intMain () { while(SCANF ("%s%lld", string,&equation)! =EOF) {Answer=0; Length=strlen (String); Search_for_answer (0,0); printf ("%d\n", Answer); }    return 0;}

HDU 2266 How many equations Can you Find (DFS)

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.