HDU4403-模拟、数学
2024-08-29 09:22:05
一道很难的奥数题,给出一个数字串,插入加号和等号使之成立。求成立的算式数。
我的做法是,先分成两段,中间插入等号 ,再分别求出左右两边可能的值和个数,然后对比,把值相等的情况乘起来,加到最终结果上。
求可能值用递归,每次遇到一个数就选择是插入加号还是不插入,不插入加号就把之前维护的值乘以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);
}
}
最新文章
- Linux Linux程序练习十八
- C#的7个原则
- 快速入门系列--WebAPI--03框架你值得拥有
- codeforces118D. Caesar&#39;s Legions
- Oracle Temp表空间切换
- Eclipse中使用SVN
- 源码搭建SVN+Apache+Setpass
- Poj(1459),最大流,EK算法
- discuz核心函数库function_core的函数注释
- pecl安装php的ev扩展时的报错处理
- C# 异步Socket
- 让c#的exe只要被修改就无法运行,支持混淆和数字证书
- 知识点练习day9
- 免费ssl证书申请和在IIS上启用https的使用教程
- 使用 docker-compose 快速安装Jenkins
- PageRank算法实现
- 20165237 2017-2018-2 《Java程序设计》第4周学习总结
- vue js库的条件渲染
- 学习ML.NET(3): 导入数据集
- Web 前端面试题整理(不定时更新)