F - Restoring the Expression CodeForces - 898F
字符串hash: base设置为10
枚举'='可能出现的位置,从1/2处开始到大概1/3处结束,当然大概的1/3不用计算,直接到最后就行,因为本题必然有解,输出直接结束即可。
根据'='号位置,'+'最多有四种位置,因为 等式的和位数确定,有进位和不进位,左和右,最多2X2,然后剪掉j的非法位置(这里没计算除了len=3以外的j有无非法位置的可能,剪了再说)
比较需要注意的地方是等式中非个位数字不能以'0'开头,开始只以为不以'0'开头即可,WA在了 1+0=0 ,改了j后又WA了0+0=0
代码
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <map>
using namespace std; typedef long long ll;
map< ll ,ll > done;
map<ll ,int > mp;
const ll MOD=1e9+;
const int maxLen=1e6+;
char s[maxLen];
ll P[maxLen];
ll sum[maxLen]; template<class T>
inline bool scan_d(T &ret){
char c; int sgn;
if(c=getchar(),c==EOF) return ;//EOF
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret=ret*+(c-'');
ret*=sgn;
return ;
} inline void out(int x){
if(x>) out(x/);
putchar(x%+'');
} ll gcd(ll a,ll b){
return b==?a:gcd(b,a%b);
} ll Qpow(ll a,ll n){
ll ret=;
ll tmp=a%MOD;
while(n){
if(n&) ret=(ret*tmp)%MOD;
tmp=(tmp*tmp)%MOD;
n>>=;
}
return ret;
} ll getF(ll t){
if(t==) return mp[t]=;
if(mp.count(t)) return mp[t];
mp[t]=Qpow(,t-);
for(int i=;i*i<=t;++i){
if(t%i==){
mp[t]=(mp[t]-getF(i)+MOD)%MOD;
if(i*i!=t) mp[t]=(mp[t]-getF(t/i)+MOD)%MOD;
}
}
return mp[t]=(mp[t]-getF()+MOD)%MOD;
} int main()
{
P[]=;
for(int i=;i<maxLen;++i){
P[i]=P[i-]*%MOD;
}
scanf("%s",s+);
int len=strlen(s+);
sum[]=;
for(int i=;i<=len;++i){
sum[i]=(sum[i-]*+s[i]-'')%MOD;
}
//for(int i=1;i<=len;++i)
// printf("%d : %I64d\n",i,sum[i]);
int flag=;
for(int i=len/;i<len;++i){
if(s[i+]==''&&len-i>) continue;
int sumlen=len-i;
for(int j:{sumlen,sumlen-,i-sumlen,i-sumlen+}){
if(j>=i||j<) continue;
if(s[j+]==''&&i-j>) continue; //printf("i : %d j : %d\n",i,j);
ll a=(sum[j]-sum[]*P[j]%MOD+MOD)%MOD;
ll b=(sum[i]-sum[j]*P[i-j]%MOD+MOD)%MOD;
ll c=(sum[len]-sum[i]*P[len-i]%MOD+MOD)%MOD;
if((a+b)%MOD==c%MOD){
for(int k=;k<=j;++k)
putchar(s[k]);
putchar('+');
for(int k=j+;k<=i;++k)
putchar(s[k]);
putchar('=');
for(int k=i+;k<=len;++k)
putchar(s[k]);
puts("");
flag=;
break;
}
}
if(flag) break;
}
return ;
}
F - Restoring the Expression CodeForces - 898F的更多相关文章
- Codeforces Round #451 (Div. 2) [ D. Alarm Clock ] [ E. Squares and not squares ] [ F. Restoring the Expression ]
PROBLEM D. Alarm Clock 题 OvO http://codeforces.com/contest/898/problem/D codeforces 898d 解 从前往后枚举,放进 ...
- codeforces 898F Hash
F. Restoring the Expression time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 898F - Restoring the Expression(字符串hash)
898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...
- Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression
题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判 ...
- F - Make It Equal CodeForces - 1065C
题目大意:有n座塔,塔高h[i],每次给定高度H对他们进行削切,要求每次削掉的所有格子数不能超过k个,输出最少削几次才能使所有塔的高度相同. 思路一:差分+贪心 对于每一个高度h,用一个数组让1~h的 ...
- Codeforces 884f F. Anti-Palindromize
题 OvO http://codeforces.com/contest/884/problem/F (Educational Codeforces Round 31 - F) 884f 解 题目标签 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces Round# 305 (Div 1)
[Codeforces 547A] #include <bits/stdc++.h> #define maxn 1000010 using namespace std; typedef l ...
- Expression Trees
Expression Trees 只是想简单说下表达式树 - Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译 ...
随机推荐
- centos7下安装python3.7
记录在2018年最后一个工作日: Linux环境坑爹得要死,环境本身有python2和python3.7两个版本:安装django2的时候,发现默认是python2:把python软连接到python ...
- axios请求带上cookie配置
Axios.defaults.withCredentials = true 参考:https://segmentfault.com/a/1190000008872646
- 自动配置IE代理脚本
http://www.cnblogs.com/ttyp/archive/2005/11/18/279124.html
- [原]vue - webapp 返回无效 解决方案
- css- 范围选择
1.子元素范围选择 举例 .iconList_wr li:nth-child(n + 1):nth-child(-n + 4) { margin-right: 0.6rem; } .iconList_ ...
- 用Hadoop AVRO进行大量小文件的处理(转)
使用 使用使用 使用 HDFS 保存大量小文件的缺点:1.Hadoop NameNode 在内存中保存所有文件的“元信息”数据.据统计,每一个文件需要消耗 NameNode600 字节内存.如果需要保 ...
- 剑指offer例题——裴波那契数列
编程题:大家都知道裴波那契数列,现在要求输入一个整数n,请你输出裴波那契数列的第n项(从0开始,第0项为0).n<=39 public class Solution { public int F ...
- 在CentOS7中利用yum命令安装mysql
在CentOS7中利用yum命令安装mysql 原创 2016年08月31日 10:42:33 标签: mysql / centos 4832 一.说明 我们是在VMware虚拟机上安装的mysql, ...
- [Nginx]Nginx的基本配置与优化1(完整配置示例与虚拟主机配置)
---------------------------------------------------------------------------------------- 完整配置示例: [ n ...
- thymeleaf 的标准表达式