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 ...
随机推荐
- OpenLayer 3 鹰眼控件和全屏显示
<body> <div id="map"></div> <script> var map=new ol.Map({ target:& ...
- [转]使用Openssl的AES加密算法
转自:http://www.thinkemb.com/wordpress/?p=18 参考:http://blog.csdn.net/shuanyancao/article/details/89859 ...
- hbase建索引的两种方式
转载自http://blog.csdn.net/ryantotti/article/details/13295325 在二级索引的实现技术上一般有几个方案: 1. 表索引 使用单独的hbas ...
- Linux一些命令
1.查看系统安装软件 rpm -qa //(不包括绿色安装的软件程序,也就是直接在安装目录中启动的不包括) rpm -qa |grep gcc 参数解释:q ——询问 a —— 查询全部 l — ...
- 基于IDL 的WebRS系统设计图
图1 用例图 图2 结构图
- 关于DOMContentLoaded
也许还有朋友不太清楚DOMContentLoaded这个事件.简单的说,这个事件就是要在大多数情况下去替代window.onload事件,因为window.onload事件必须要等待页面所有元素的资源 ...
- 性能更好的js动画实现方式---requestAnimationFrame
用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css3动画出来后,我们又可以使用css3来实现动画了,而且性能和流畅度也得到了很大的提升.但是css3动画还是 ...
- RadioGroup+TabHost
=.= //MainActivity public class MainActivity extends TabActivity implements OnCheckedChangeListener ...
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下 PackageManager packageManager = this.getPackageManag ...
- They Are Everywhere
They Are Everywhere Sergei B., the young coach of Pokemons, has found the big house which consists o ...