たくさんの数式 / Many Formulas AtCoder - 2067 (枚举二进制)
Problem Statement
You are given a string S consisting of digits between 1 and 9, inclusive. You can insert the letter + into some of the positions (possibly none) between two letters in this string. Here, + must not occur consecutively after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results.
Constraints
- 1≤|S|≤10
- All letters in S are digits between
1and9, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the sum of the evaluated value over all possible formulas.
Sample Input 1
125
Sample Output 1
176
There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,
- 125
- 1+25=26
- 12+5=17
- 1+2+5=8
Thus, the sum is 125+26+17+8=176.
Sample Input 2
9999999999
Sample Output 2
12656242944 思路:
每两个数之间的是否加“+“只有两种状态,即加和不加,那么字符串的最大长度是10,我们来用二进制思想来枚举所有状态,时间复杂度也就是2^n
显然不会TLE,用字符串substr函数来分割字符串,stringstream来转成int数字,写起来会很方便。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll getx(string str)
{
if(str=="")
{
return 0ll;
}
stringstream ss;
ss.clear();
ss<<str;
ll res;
ss>>res;
return res;
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
string s;
cin>>s;
int len=s.length();
ll ans=0ll;
std::vector<string> v;
for(int i=;i<=(<<(len-));i++)
{
v.clear();
int num=;
for(int j=;j<=(len-);j++)
{
if((i&(<<j)))
{
string temp=s.substr(len--j,num);
v.push_back(temp);
num=;
}else
{
num++;
}
}
string temp=s.substr(,num-);
v.push_back(temp);
// ll tt=0ll;
for(auto x:v)
{
// db(x);
ans+=getx(x);
} }
cout<<ans<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
たくさんの数式 / Many Formulas AtCoder - 2067 (枚举二进制)的更多相关文章
- AtCoder Beginner Contest 045 C - たくさんの数式 / Many Formulas
Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given a string ...
- Java 8 新特性1-函数式接口
Java 8 新特性1-函数式接口 (原) Lambda表达式基本结构: (param1,param2,param3) -> {代码块} 例1: package com.demo.jdk8; i ...
- Java 8 新特性:1-函数式接口
(原) Java 8 新特性1-函数式接口 Lambda表达式基本结构: (param1,param2,param3) -> {代码块} Lambda表达式结构: (type1 arg1,typ ...
- EBS OAF开发中实现參数式弹出窗体
EBS OAF开发中实现參数式弹出窗体 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 概览 參数式弹出窗体和嵌入式弹出窗体不一样,它拥有独立 ...
- Java-函数式编程(二)Lambda表达式
本文首发:Java-函数式编程(二)Lambda表达式 “Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lamb ...
- Java-函数式编程(一)初识篇
开发者使用Java8编写复杂的集合处理算法,只需要简单的代码就能在多喝cpu上高效运行,这就是Lambda表达式的初衷. 提示:函数式编程和语言无关,它是一种思想,任何语言都可以实现函数式编程,区别只 ...
- [Google Guava] 4-函数式编程
原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所改变,但Guava现在就想给JDK5以上用户提 ...
- poj 3977 Subset(折半枚举+二进制枚举+二分)
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5721 Accepted: 1083 Descripti ...
- USACO2.1 Hamming Codes【枚举+二进制处理+输出格式+题意理解】
这道题加了2个看起来奇奇怪怪的$tag$ 1.输出格式:不得不说这个格式输出很恶心,很像$UVA$的风格,细节稍微处理不好就会出错. 因为这个还$WA$了一次: ,m=n; ) { ;i<=t+ ...
随机推荐
- JaveWeb学习之Servlet(一):Servlet生命周期和加载机制
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-22/19.html 作者:夜月归途 出处:http://www.guitu ...
- 【.NET Core项目实战-统一认证平台】第十一章 授权篇-密码授权模式
[.NET Core项目实战-统一认证平台]开篇及目录索引 上篇文章介绍了基于Ids4客户端授权的原理及如何实现自定义的客户端授权,并配合网关实现了统一的授权异常返回值和权限配置等相关功能,本篇将介绍 ...
- 微服务定义及.Net Core中用的技术
微服务 定义: 它是一种架构模式,提倡将大的单体系统,按业务拆分成一个个较小且独立的服务,服务与服务之前进行相互协作和配合. 历史: 针对互联网行业的蓬勃发展,需要支撑的业务越来越多,越来越大,单体程 ...
- 生产者消费者C++实现
#include<string>#include<iostream>#include<process.h>#include<windows.h>#inc ...
- 由AbstractQueuedSynchronizer和ReentrantLock来看模版方法模式
在学完volatile和CAS之后,近几天在撸AbstractQueuedSynchronizer(AQS)的源代码,很多并发工具都是基于AQS来实现的,这也是并发专家Doug Lea的初衷,通过写一 ...
- 使用 CODING 进行 Hexo 项目的持续集成
本文作者:CODING 用户 - 廖石荣 关于持续集成的概念 持续集成指的是,频繁地(一天多次)将代码集成到主干. 持续集成的过程 如图所示: CI 过程:代码编写 -> 源代码库(GitHub ...
- 基于GIS的视频管理指挥平台
平台利用空间地理信息技术,以GIS地图为基础,将各类信息空间化.可视化,实现基于空间电子地图的可视化查询和分析,它能使情报.推理.分析与其他可用数据融为一体,提供依托于电子地图的清晰而精确的现场态势图 ...
- Webpack4教程:第一部分,入口、输入和ES6模块
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://wanago.io/2018/07/16/webpack-4-course-par ...
- C#监控指定目录的文件变化的代码
如下的资料是关于C#监控指定目录的文件变化的代码. FileSystemWatcher watcher = new FileSystemWatcher();watcher.Path = @" ...
- (转)hibernateTools工具安装及使用总结(eclipse 3.6)
最近项目采用flex+spring+hibernate的框架开发,之前虽说有多年的Java开发经验了,但是一直使用的JDBC或者 ibatis,hibernate的使用还是大姑娘上轿头一回,网上都介绍 ...