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

代码

  1 #include <iostream>
  2 #include <cmath>
  3 #include <algorithm>
  4 #include <vector>
  5 #include <cstring>
  6 #include <queue>
  7 #include <map>
  8 using namespace std;
  9
 10 typedef long long ll;
 11 map< ll ,ll > done;
 12 map<ll ,int > mp;
 13 const ll MOD=1e9+7;
 14 const int maxLen=1e6+3;
 15 char s[maxLen];
 16 ll P[maxLen];
 17 ll sum[maxLen];
 18
 19 template<class T>
 20 inline bool scan_d(T &ret){
 21     char c; int sgn;
 22     if(c=getchar(),c==EOF) return 0;//EOF
 23     while(c!=‘-‘&&(c<‘0‘||c>‘9‘)) c=getchar();
 24     sgn=(c==‘-‘)?-1:1;
 25     ret=(c==‘-‘)?0:(c-‘0‘);
 26     while(c=getchar(),c>=‘0‘&&c<=‘9‘) ret=ret*10+(c-‘0‘);
 27     ret*=sgn;
 28     return 1;
 29 }
 30
 31 inline void out(int x){
 32     if(x>9) out(x/10);
 33     putchar(x%10+‘0‘);
 34 }
 35
 36 ll gcd(ll a,ll b){
 37     return b==0?a:gcd(b,a%b);
 38 }
 39
 40 ll Qpow(ll a,ll n){
 41     ll ret=1;
 42     ll tmp=a%MOD;
 43     while(n){
 44         if(n&1) ret=(ret*tmp)%MOD;
 45         tmp=(tmp*tmp)%MOD;
 46         n>>=1;
 47     }
 48     return ret;
 49 }
 50
 51 ll getF(ll t){
 52     if(t==1) return mp[t]=1;
 53     if(mp.count(t)) return mp[t];
 54     mp[t]=Qpow(2,t-1);
 55     for(int i=2;i*i<=t;++i){
 56         if(t%i==0){
 57             mp[t]=(mp[t]-getF(i)+MOD)%MOD;
 58             if(i*i!=t) mp[t]=(mp[t]-getF(t/i)+MOD)%MOD;
 59         }
 60     }
 61     return mp[t]=(mp[t]-getF(1)+MOD)%MOD;
 62 }
 63
 64 int main()
 65 {
 66     P[0]=1;
 67     for(int i=1;i<maxLen;++i){
 68         P[i]=P[i-1]*10%MOD;
 69     }
 70     scanf("%s",s+1);
 71     int len=strlen(s+1);
 72     sum[0]=0;
 73     for(int i=1;i<=len;++i){
 74         sum[i]=(sum[i-1]*10+s[i]-‘0‘)%MOD;
 75     }
 76     //for(int i=1;i<=len;++i)
 77     //    printf("%d : %I64d\n",i,sum[i]);
 78     int flag=0;
 79     for(int i=len/2;i<len;++i){
 80         if(s[i+1]==‘0‘&&len-i>1) continue;
 81         int sumlen=len-i;
 82         for(int j:{sumlen,sumlen-1,i-sumlen,i-sumlen+1}){
 83             if(j>=i||j<1) continue;
 84             if(s[j+1]==‘0‘&&i-j>1) continue;
 85
 86         //printf("i : %d   j : %d\n",i,j);
 87             ll a=(sum[j]-sum[0]*P[j]%MOD+MOD)%MOD;
 88             ll b=(sum[i]-sum[j]*P[i-j]%MOD+MOD)%MOD;
 89             ll c=(sum[len]-sum[i]*P[len-i]%MOD+MOD)%MOD;
 90             if((a+b)%MOD==c%MOD){
 91                 for(int k=1;k<=j;++k)
 92                     putchar(s[k]);
 93                 putchar(‘+‘);
 94                 for(int k=j+1;k<=i;++k)
 95                     putchar(s[k]);
 96                 putchar(‘=‘);
 97                 for(int k=i+1;k<=len;++k)
 98                     putchar(s[k]);
 99                 puts("");
100                 flag=1;
101                 break;
102             }
103         }
104         if(flag) break;
105     }
106     return 0;
107 }

原文地址:https://www.cnblogs.com/Kiritsugu/p/9321192.html

时间: 2024-07-30 16:40:09

F - Restoring the Expression CodeForces - 898F的相关文章

Codeforces Round #451 (Div. 2) F Restoring the Expression

题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判断 我分了5种情况 0.开头字符为0, 那么结果一定是0+a=a的形式 然后4种情况 1.len(a) >= len(b) 且 len(c) == len(a) 2.len(a) <= len(b) 且 len(c) == len(b) 3.len(a) >= len(b) 且 len(c)

Codeforces 898F 字符串hash

F. Restoring the Expression 题意:给出一个字符串,要把它折分成三部分 a.b.c , 使得 a+b=c .输出任何一种可行情况. tags:字符串 hash 因为 a+b=c ,所以 lena.lenb 至少要有一个等于 lenc 或 lenc-1  .所以枚举 lenc,每次检验一下可不可行. 但每次都暴力检验肯定超时,这里把字符串hash 一下,根据 hash值 快速检验. 记一下模板: const int mod = 1e9+7; ll Hash[N], p[N

F - Restoring Table(按位与,按位或)

这题坑爹..按位没学好.&是同为1,结果才是1,而|是同为0,结果才是0,这样num1&num2,num1&num3,num1&num4,得到的结果里的二进制含有1的位数,则在原数num1和num2.3.4里肯定是1,含有0的位数,要么在原数中是0,要么在num2.3.4中是0,或者都是0 所以用|运算,把有1的位数(原数中肯定此位是1)都先还原回去,然后多次操作叠加起来.就能还原出一组可能的数据了(注意:只是可能的一组数据,因为会有多组解,无法都找到) F - Resto

F - Lost Root Shows CodeForces - 1061F(交互+概率)

Lost Root time limit per test 3 seconds memory limit per test 256 megabytes Problem Description The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some vertex. Then tree is called to be perfect kk-ary tree if

F - Make It Equal CodeForces - 1065C

题目大意:有n座塔,塔高h[i],每次给定高度H对他们进行削切,要求每次削掉的所有格子数不能超过k个,输出最少削几次才能使所有塔的高度相同. 思路一:差分+贪心 对于每一个高度h,用一个数组让1~h的数,每一个都加一.用差分求一下后缀和可以完成. AC code: #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll N=2E5+7; ll arr[N]; int main() { ll n,k;

F - Link/Cut Tree CodeForces - 614A(水题)

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition of this procedure

Educational Codeforces Round 76 F 折半枚举

Educational Codeforces Round 76 F 折半枚举 https://codeforces.com/contest/1257/problem/F 题意: 数组a,找到一个x使得a中每一个元素异或x后"二进制中1的个数"相同. 数组长度100,数字大小2^30. 思路: 折半枚举答案X,如分为X前15位和后15位.之后我们再枚举期望的"相同个数"是多少,通过hash看看能不能满足就好了. 代码: #include <bits/stdc++

codeforces 的20道C题

A - Warrior and Archer CodeForces - 595C n  偶数  然后n个数字 A B 轮流取一个 A让差变小B让差变大 直到最后2 个   求的是最小剩下的差 最后剩下的 L R  相距 n/2    求一下最小的就行 #include <iostream> #include <cstdio> #include <cmath> #include <map> #include <algorithm> #include

[C#] C# 知识回顾 - 表达式树 Expression Trees

C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达式树 调试 简介 表达式树以树形数据结构表示代码,其中每一个节点都是一种表达式,比如方法调用和 x < y 这样的二元运算等. 你可以对表达式树中的代码进行编辑和运算.这样能够动态修改可执行代码.在不同数据库中执行 LINQ 查询以及创建动态查询. 表达式树还能用于动态语言运行时 (DLR) 以提供