CodeForces 625D Finals in arithmetic
神奇的构造题,我的思路比较奇葩。搞了好久,看到WA on 91我绝望了,然后自己造数据,找到了错误,总算是AC了,现在是凌晨0:24分,看到AC之后,感动China!
我写的代码无比的长。。。。。应该有很简单的方法吧。。。。。没想到。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std; const int maxn = + ;
char s[maxn];
int a[maxn], b[maxn], sum[maxn], flag[maxn];
int tmpa[maxn], tmpb[maxn], ans[maxn];
int nb[maxn];
int len; void read()
{
scanf("%s", s);
for (int i = ; s[i]; i++) b[i + ] = s[i] - '';
nb[] = b[] * + b[]; for (int i = ; s[i]; i++) nb[i] = s[i] - '';
} void init()
{
len = strlen(s);
memset(flag, , sizeof flag);
} bool check1()
{
for (int i = ; i <= (len + ) / ; i++)
{
if (sum[i] % == ) a[i] = sum[i] / , a[len - i + ] = sum[i] / ;
else a[i] = sum[i] / + , a[len - i + ] = sum[i] - a[i];
} for (int i = ; i <= len; i++) tmpa[i] = a[i];
for (int i = ; i <= len; i++) tmpb[i] = a[len - i + ]; int k = ;
for (int i = ; i <= len; i++)
{
ans[i] = (tmpa[i] + tmpb[i] + k) % ;
k = (tmpa[i] + tmpb[i] + k) / ;
} for (int i = ; i <= len; i++)
if (b[len - i + ] != ans[i]) return ;
return ;
} bool work1()
{
bool fail = ; init(); for (int i = len; i >= len / + ; i--)
{
int id_now = i, id_pre = len - i + ; if (i == len)
{
if (b[i] == ) { fail = ; break; } sum[id_now] = b[id_now]; sum[id_pre] = b[id_now];
flag[id_now] = ; flag[id_pre] = ; if (b[id_now] == b[id_pre]) flag[id_pre + ] = ;
else if (b[id_now] + == b[id_pre]) flag[id_pre + ] = ;
else { fail = ; break; }
} else
{
int num_now, num_pre;
num_now = b[id_now] - flag[id_now + ]; if (num_now == )
{
if (b[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else if (b[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else { fail = ; break; }
}
else if (b[id_now] == && flag[id_now + ])
{
if (b[id_pre] == || b[id_pre] == )
{
flag[id_now] = ;
if (b[id_pre] == ) flag[id_pre + ] = ;
sum[id_now] = ;
sum[id_pre] = ;
}
else { fail = ; break; }
}
else
{
num_now = num_now + flag[id_pre] * ;
num_pre = b[id_pre] + flag[id_pre] * ; if (num_now == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
} else if (num_now + == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
}
else { fail = ; break; }
}
}
if (id_now == id_pre&&sum[id_pre] % != ) { fail = ; break; }
} if (!check1()) fail = ; return fail;
} bool check2()
{
memset(tmpa, , sizeof tmpa);
memset(tmpb, , sizeof tmpb); for (int i = ; i <= (len + ) / ; i++)
{
if (sum[i] % == ) a[i] = sum[i] / , a[len - i + ] = sum[i] / ;
else a[i] = sum[i] / + , a[len - i + ] = sum[i] - a[i];
} for (int i = ; i <= len; i++) tmpa[i] = a[i];
for (int i = ; i <= len; i++) tmpb[i] = a[len - i + ]; int k = ;
len++;
for (int i = ; i <= len; i++)
{
ans[i] = (tmpa[i] + tmpb[i] + k) % ;
k = (tmpa[i] + tmpb[i] + k) / ;
} for (int i = ; i <= len; i++)
if (b[len - i + ] != ans[i]) return ;
return ;
} bool work2()
{
bool fail = ; init(); len--; for (int i = len; i >= len / + ; i--)
{
int id_now = i, id_pre = len - i + ; if (i == len)
{
sum[id_now] = + nb[id_now]; sum[id_pre] = + nb[id_now];
flag[id_now] = ; flag[id_pre] = ; if (nb[id_now] + == nb[id_pre]) flag[id_pre + ] = ;
else if (nb[id_now] + + == nb[id_pre]) flag[id_pre + ] = ;
else { fail = ; break; }
} else
{
int num_now, num_pre;
num_now = nb[id_now] - flag[id_now + ]; if (num_now == )
{
if (nb[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else if (nb[id_pre] == )
{
sum[id_now] = ; sum[id_pre] = ;
flag[id_pre + ] = ;
}
else { fail = ; break; }
}
else if (nb[id_now] == && flag[id_now + ])
{
if (nb[id_pre] == || nb[id_pre] == )
{
flag[id_now] = ;
if (nb[id_pre] == ) flag[id_pre + ] = ;
sum[id_now] = ;
sum[id_pre] = ;
}
else { fail = ; break; }
}
else
{
num_now = num_now + flag[id_pre] * ;
num_pre = nb[id_pre] + flag[id_pre] * ; if (num_now == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
} else if (num_now + == num_pre)
{
flag[id_pre + ] = ;
sum[id_now] = num_now; sum[id_pre] = num_now;
if (num_now >= ) flag[id_now] = ;
}
else { fail = ; break; }
}
}
if (id_now == id_pre&&sum[id_pre] % != ) { fail = ; break; }
} if (!check2()) fail = ; return fail;
} int main()
{
read();
init();
if (!work1())
{
for (int i = ; i <= len; i++) printf("%d", a[i]);
printf("\n");
}
else
{
if (b[] == )
{
if (!work2())
{
for (int i = ; i <= len - ; i++) printf("%d", a[i]);
printf("\n");
}
else { printf("0\n"); }
}
else { printf("0\n"); }
}
return ;
}
CodeForces 625D Finals in arithmetic的更多相关文章
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心
D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)
传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...
- 【Codeforces 115D】Unambiguous Arithmetic Expression
Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...
- CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression
题目链接:https://codeforces.com/contest/978/problem/D 题解: 题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对 ...
- 【codeforces 604D】Moodular Arithmetic
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 710 D. Two Arithmetic Progressions
Description \(x=a_1k+b_1=a_2l+b_2,L\leqslant x \leqslant R\) 求满足这样条件的 \(x\) 的个数. Sol 扩展欧几里得+中国剩余定理. ...
- Codeforces Round #342 (Div. 2)
贪心 A - Guest From the Past 先买塑料和先买玻璃两者取最大值 #include <bits/stdc++.h> typedef long long ll; int ...
- Codeforces Round #342 (Div 2) 解题报告
除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
随机推荐
- 【单源最短路模板】 poj 2387
#include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...
- C# 引用参数
最近经常和同事讨论引用参数的问题,为了搞清楚,查了些资料,其中CLR via C#中讲的比较清楚,整理了下 ----摘自(CLR via C#) 在默认情况下,CLR假设所有的方法参数都是按值传递的. ...
- CodeForces 510B DFS水题
题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<c ...
- lucene特殊字符处理
这是个郁闷的问题,今天遇到了,但在lucene中查询的关键字保护有特殊字符,譬如--,会出现如下异常: org.apache.lucene.queryParser.ParseException: Ca ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- HDU - 1205 I NEED A OFFER!
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题意: 该题要求得到一份offer的最大概率,在例子中的0.44 = 1-(1-0.2)*(1- ...
- 转:jmeter实践
本文主要介绍性能测试中的常用工具jmeter的使用方式,以方便开发人员在自测过程中就能自己动手对系统进行自动压测和模拟用户操作访问请求.最后还用linux下的压测工具ab做了简单对比. 1. ...
- 转:WebDriver进行屏幕截图
例: 打开百度首页 ,进行截图 01 packagecom.example.tests; 02 importjava.io.File; 03 importorg.apache.commons.io ...
- 优化之zencart第一时间修改原始内容
Zen Cart 基本修改指南 Zen Cart,全球顶级B2C商城网站!要想自行搭建一个基本的Zen Cart的网站,这篇文章是绝对不能错过的.目前我已经做了两个B2C网站,但是还是离不开这篇文章的 ...
- Java 学习路线以及各阶段学习书籍,博文,视频的分享
感谢: 感谢每一个打开这篇文章的人,听我在这里瞎扯!至于我为什么会有闲情写这篇文章呢?因为我每天想的是为什么要给我这样的需求,背后的人性是什么,我能再做些什么能让他更好.久而久之,我也稍微有了些自己的 ...