How Many Equations Can You Find

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

Description

Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.

Input

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

Output

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

Sample Input

123456789 3 21 1

Sample Output

18 1

简单翻译:

给你两个数n,m.你需要在n的中间填上加号或减号,或者不填。让n这个部分的值等于m。问有多少种方案。

举个例子:n=12345.

你可以操作它,使它变成这样 123+4-5=122。

解题思路:

dfs,考虑已经处理的数字的个数和当前这部分的值,处理到终点时,如果等于m,结果+1.

代码:

#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
char String[];
LL Equation;
int Length,Answer;
void Search_For_Answer(int Now_Position,LL Now_Value)
{
if(Now_Position>=Length)
{
if(Now_Value==Equation) Answer++;
return;
}
LL Temp_Value=;
for(int i=;i<=Length-Now_Position;i++)
{
Temp_Value=Temp_Value*+String[i-+Now_Position]-'';
Search_For_Answer(Now_Position+i,Now_Value+Temp_Value);
if(Now_Position)
Search_For_Answer(Now_Position+i,Now_Value-Temp_Value);
}
}
int main()
{
while(scanf("%s%lld",String,&Equation)!=EOF)
{
Answer=;
Length=strlen(String);
Search_For_Answer(,);
printf("%d\n",Answer);
}
return ;
}

HDU 2266 How Many Equations Can You Find(DFS)的更多相关文章

  1. HDOJ(HDU).2266 How Many Equations Can You Find (DFS)

    HDOJ(HDU).2266 How Many Equations Can You Find (DFS) [从零开始DFS(9)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零 ...

  2. hdu - 2266 How Many Equations Can You Find (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2266 给一个字符串和一个数n,在字符串中间可以插入+或者 -,问有多少种等于n的情况. 要注意任意两个数之间都可 ...

  3. HDU 2266 How Many Equations Can You Find(模拟,深搜)

    题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string ...

  4. hdu 2266 dfs+1258

    How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. 【HDOJ】2266 How Many Equations Can You Find

    简单DFS. #include <cstdio> #include <cstring> #define MAXN 15 char str[MAXN]; __int64 x; i ...

  6. hdu 2266 dfs

    题意:在数字之间添加运算符号,使得结果等于题目中要求的Sample Input123456789 321 1Sample Output181 这题虽然看起来比较简单,但是之前和差的状态不太好表示,因此 ...

  7. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. How Many Equations Can You Find(dfs)

    How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. SGU 294 He's Circles

    题意:一个项链有n个珠子,每个珠子为黑色或白色.问有多少种不同的项链? 注意,n的数量十分大,因此,我们枚举i(1<=i<=n),令L=n/i,求出L的欧拉函数,则这些数和L互质,因此gc ...

  2. 05_Elasticsearch 单模式下API的增删改查操作

    05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...

  3. Android Studio代码着色插件

    文章将给大家分享Studio中代码高亮插件,个人觉得换个代码着色方式还是挺有必要的,起码让视觉上有个变换,感官上爽一些.就像吃惯了大鱼大肉,偶尔也来点青菜萝卜吧.以下是个人喜欢的几款,给个效果图大家看 ...

  4. makefile例子《一》

    一.例子 (1)makefile和src源文件不在同一目录下 (2)把.o生成到指定目录下 文件结构目录 ----inc      //放头文件 ----lib //放所需要的.a或者.so文件 -- ...

  5. FlexComboBoxTree

    在我的CSDN资源中有项目工程文件.下载导入工程即可看到效果,下面是地址. http://download.csdn.net/detail/cym_lmy/6326053 MyCombBoxTree1 ...

  6. Linux各种包安装及命令

    1.Locate yum -y install mlocate 若出现问题: locate: can not stat () `/var/lib/mlocate/mlocate.db': 没有那个文件 ...

  7. [转]Laravel 4之请求

    Laravel 4之请求 http://dingjiannan.com/2013/laravel-request/ 获取请求数据 获取当前请求所包括的所有GET和POST数据 Route::get(' ...

  8. HDU 1085 Holding Bin-Laden Captive! (母函数)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. SQLLoader4(数据文件中的列与表中列不一致情况-filler)

    A.数据文件中字段个数少于表中列字段个数,但数据文件中缺少的列,在表定义中可以为空.----- 这种情况是比较简单的,只需要将数据文件中数据对应的列的名字写到控制文件中即可.因为SQL*Loader是 ...

  10. EXPDP和IMPDP简单测试

    一.EXPDP和IMPDP使用说明  Oracle Database 10g引入了最新的数据泵(Data Dump)技术,数据泵导出导入(EXPDP和IMPDP)的作用 1)实现逻辑备份和逻辑恢复.  ...