Codeforces #451 Div2 F

时间:2022-09-16 12:55:51

#451 Div2 F

题意

给出一个由数字组成的字符串,要求添加一个加号和等号,满足数字无前导 0 且等式成立。

分析

对于这种只有数字的字符串,可以快速计算某一区间的字符串变成数字后并取模的值,首先从右到左,将字符串转化为数字并取模,那么 \(h[i]\) 表示字符串 \(S[i...len]\) 转化成数字后并取模的值,如果要求区间 \([i, j]\) 所表示的数字是多少,首先求出 \(h[i] - h[j + 1]\),后面的 0 可以除掉,求一下逆元即可。

然后枚举一下,比一下就行了。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;
int len;
char s[N], z[N];
ll h[N], b[N], inv[N];
ll POW(ll x, ll k) {
ll r = 1;
while(k) {
if(k & 1) r = r * x % MOD;
x = x * x % MOD;
k >>= 1;
}
return r;
}
ll getval(int l, int r) {
return ((h[l] - h[r + 1]) * inv[len - r - 1] % MOD + MOD) % MOD;
}
int solve(int l1, int l2, int l3) {
int pos1 = 0, pos2 = l1, pos3 = len - l3;
if((!s[pos1] && l1 != 1) || (!s[pos2] && l2 != 1) || (!s[pos3] && l3 != 1)) return 0;
if((getval(pos1, pos2 - 1) + getval(pos2, pos3 - 1)) % MOD == getval(pos3, len - 1)) {
int mnl = min(l1, l2), mxl = max(l1, l2), nl = 0, y = 0;
for(int i = 0; i < mnl; i++) {
z[nl] = (s[pos2 - 1 - i] + s[pos3 - 1 - i] + y) % 10;
y = (s[pos2 - 1 - i] + s[pos3 - 1 - i] + y) / 10;
if(len - nl - 1 < 0 || s[len - nl - 1] != z[nl]) return 0;
nl++;
}
int np = (l1 < l2 ? pos3 : pos2);
for(int i = mnl; i < mxl; i++) {
z[nl] = (s[np - 1 - i] + y) % 10;
y = (s[np - 1 - i] + y) / 10;
if(len - nl - 1 < 0 || s[len - nl - 1] != z[nl]) return 0;
nl++;
}
if(y) {
z[nl] = y;
if(len - nl - 1 < 0 || s[len - nl - 1] != z[nl]) return 0;
nl++;
}
if(nl == l3) {
for(int i = 0; i < l1; i++) printf("%d", s[i]); printf("+");
for(int i = 0; i < l2; i++) printf("%d", s[pos2 + i]); printf("=");
for(int i = 0; i < l3; i++) printf("%d", s[pos3 + i]); printf("\n");
return 1;
}
}
return 0;
}
int main() {
scanf("%s", s);
len = strlen(s);
b[0] = 1;
inv[0] = 1;
s[0] -= '0';
for(int i = 1; i < len; i++) {
b[i] = b[i - 1] * 10 % MOD;
s[i] -= '0';
inv[i] = POW(b[i], MOD - 2);
}
for(int i = len - 1; i >= 0; i--) {
h[i] = (h[i + 1] + b[len - 1 - i] * s[i]) % MOD;
}
for(int i = len / 3; i < len; i++) {
int l3 = i, l2 = i, l1 = len - 2 * i;
if(l1 > 0 && l3 >= l1 && l3 >= l2) {
if(solve(l1, l2, l3)) return 0;
if(solve(l2, l1, l3)) return 0;
}
l2 = i - 1, l1 = len - l3 - l2;
if(l1 > 0 && l3 >= l1 && l3 >= l2) {
if(solve(l1, l2, l3)) return 0;
if(solve(l2, l1, l3)) return 0;
}
}
return 0;
}

Codeforces #451 Div2 F的更多相关文章

  1. Codeforces &num;541 &lpar;Div2&rpar; - F&period; Asya And Kittens(并查集&plus;链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  2. Codeforces &num;452 Div2 F

    #452 Div2 F 题意 给出一个字符串, m 次操作,每次删除区间 \([l,r]\) 之间的字符 \(c\) ,输出最后得到的字符串. 分析 通过树状数组和二分,我们可以把给定的区间对应到在起 ...

  3. Codeforces &num;442 Div2 F

    #442 Div2 F 题意 给出一些包含两种类型(a, b)问题的问题册,每本问题册有一些题目,每次查询某一区间,问有多少子区间中 a 问题的数量等于 b 问题的数量加 \(k\) . 分析 令包含 ...

  4. Codeforces &num;528 Div2 F &lpar;1087F&rpar; Rock-Paper-Scissors Champion 树状数组&plus;set

    题意:n个人站成一排,初始时刻每个人手中都有一个图案,可能是石头,剪刀,布3个中的1种,之后会随机选取相邻的两个人玩石头剪刀布的游戏,输的人会离开(如果两个人图案相同,则随机选择一个人离开).执行(n ...

  5. Codeforces &num;548 &lpar;Div2&rpar; - D&period;Steps to One(概率dp&plus;数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. Codeforces &num;180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  7. Codeforces &num;541 &lpar;Div2&rpar; - E&period; String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  8. Codeforces &num;541 &lpar;Div2&rpar; - D&period; Gourmet choice(拓扑排序&plus;并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  9. Educational Codeforces Round 40 F&period; Runner&&num;39&semi;s Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

随机推荐

  1. js防止客户端多触发

    代码: /***防止多触发**id 必须唯一*fn 回掉函数*wait 延迟多长时间**使用例子:* ToPreventMoreTrigger('id', function () {//注意 id 是 ...

  2. cacti 添加

    一,为已有host添加新的监控图 (基于snmp) 在console控制台下点击“New Graphs”,选择要添加监控图的主机.在Graph Templates中选择一个Graph模板,本例选择SN ...

  3. C&num;操控条形码扫描枪

    // 条码扫描器 // 窗体部分相关代码: using System; using System.Collections.Generic; using System.ComponentModel; u ...

  4. &lbrack;topcoder&rsqb;IncreasingSubsequences

    http://community.topcoder.com/stat?c=problem_statement&pm=7753&rd=10672 http://community.top ...

  5. JSP 2秒跳转到首页

    <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%><h ...

  6. X-UA-Compatible IE 浏览器默认文档模式设置

    制作网页的时候,IE8浏览器浏览页面的时候,有时候文档模式默认是IE7,导致IE8兼容性不是非常好.出现IE7应该出现的模式. 解决的方法例如以下: 在X-UA-Compatible中可用的方法有: ...

  7. 关于RuntimException

    对于实现接口的类如果要抛出异常的话,那么接口也要抛出异常 所以RuntimeException只要对于实现接口的类就可以了 对于继承的类也可以这样运用 毕竟在实际开发中接口不一定是自己写的,而且团队可 ...

  8. 我们为什么要用springcloud?

    1 2 单体架构 在网站开发的前期,项目面临的流量相对较少,单一应用可以实现我们所需要的功能,从而减少开发.部署和维护的难度.这种用于简单的增删改查的数据访问框架(ORM)十分的重要.  垂直应用架构 ...

  9. module &&num;39&semi;scipy&period;misc&&num;39&semi; has no attribute &&num;39&semi;toimage&&num;39&semi;,python

    anaconda环境下: 错误:python 命令行运行出错:module 'scipy.misc' has no attribute 'toimage' 解决:打开Anaconda prompt,输 ...

  10. 超级NB的防DDOS&lpar;小量级&rpar;攻击的脚本

    # tree /usr/local/ddos/ /usr/local/ddos/ ├── ddos.conf ├── ddos.sh ├── ignore.ip.list └── LICENSE di ...