一道很难的奥数题,给出一个数字串,插入加号和等号使之成立。求成立的算式数。

我的做法是,先分成两段,中间插入等号 ,再分别求出左右两边可能的值和个数,然后对比,把值相等的情况乘起来,加到最终结果上。

求可能值用递归,每次遇到一个数就选择是插入加号还是不插入,不插入加号就把之前维护的值乘以10加上此值,插入加号把维护的值加到和里。这样可以把所有的情况存进map里。

 #include <algorithm>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> using namespace std; int N,M,T;
char save[]; map <long long,int> m[]; void solve(int l,int r,long long cur,long long combo,int d)
{
if(l >= r)
{
cur += combo;
if(m[d].count(cur) == )
m[d].insert( pair<long long,int>(cur,));
else m[d][cur]++;
return;
} solve(l+,r,cur+combo,save[l+]-'',d);
solve(l+,r,cur,combo*+save[l+]-'',d);
return;
} int main()
{
while(scanf("%s",save) && save[] != 'E')
{
int n = strlen(save);
int ans = ;
for(int i = ;i < n-;i++)
{
m[].clear();m[].clear();
solve(,i,,save[]-'',);
solve(i+,n-,,save[i+]-'',);
map<long long,int>::iterator it = m[].begin(); for(;it != m[].end();it++)
{
//printf("%d %d\n",it->first,it->second);
if(m[].count(it->first) )
ans += (it->second * m[][it->first]);
}
}
printf("%d\n",ans);
}
}

最新文章

  1. Linux Linux程序练习十八
  2. C#的7个原则
  3. 快速入门系列--WebAPI--03框架你值得拥有
  4. codeforces118D. Caesar&#39;s Legions
  5. Oracle Temp表空间切换
  6. Eclipse中使用SVN
  7. 源码搭建SVN+Apache+Setpass
  8. Poj(1459),最大流,EK算法
  9. discuz核心函数库function_core的函数注释
  10. pecl安装php的ev扩展时的报错处理
  11. C# 异步Socket
  12. 让c#的exe只要被修改就无法运行,支持混淆和数字证书
  13. 知识点练习day9
  14. 免费ssl证书申请和在IIS上启用https的使用教程
  15. 使用 docker-compose 快速安装Jenkins
  16. PageRank算法实现
  17. 20165237 2017-2018-2 《Java程序设计》第4周学习总结
  18. vue js库的条件渲染
  19. 学习ML.NET(3): 导入数据集
  20. Web 前端面试题整理(不定时更新)

热门文章

  1. 学习CSS布局 - position例子
  2. oracle if/else功能的实现的3种写法
  3. 加解密工具类(含keystore导出pfx)
  4. min-max 容斥
  5. xhtml和html的区别 html5和xhtml的区别
  6. bitcoin 源码解析 - 交易 Transaction(三) - Script
  7. 利用matplotlib的plot函数实现图像绘制
  8. STM32串口打印输出乱码的解决办法
  9. 《Linux内核设计与分析》第四章读书笔记
  10. ios开发之--CAKeyframeAnimation的详细用法