/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>
#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
using namespace std;
int classification[10][10][10];
int classify(int a, int b, int c)
{
if ((a+b)%10==c)
{
if (a+b<10) return 11;
else return 12;
}
else if ((a+b+1)%10==c)
{
if (a+b+1<10) return 21;
else return 22;
}
return 0;
}
int n;
int seq[1000005];
LL A[1000005];
LL B[1000005];
LL calculate()
{
A[n]=0; B[n]=0; int cl;
FORD(index,n-1,0)
{
cl=seq[index];
if (cl==0) { A[index]=0; B[index]=0; }
else if (cl==11) { A[index]=1+A[index+1]; B[index]=0; }
else if (cl==12) { A[index]=0; B[index]=1+A[index+1]; }
else if (cl==21) { A[index]=B[index+1]; B[index]=0; }
else if (cl==22) { A[index]=0; B[index]=B[index+1]; }
}
LL sum=0; FOR(i,0,n-1) sum+=A[i];
return sum;
}
/// MAIN
int main(int argc, char* argv[])
{
// magic formula, which makes streams work faster:
ios_base::sync_with_stdio(0);
FOR(i,0,9) FOR(j,0,9) FOR(k,0,9) classification[i][j][k]=classify(i,j,k);
string s1, s2, s3;
cin >> s1; cin >> s2; cin >> s3;
n=s1.size();
FOR(i,0,n-1)
{
int d1=s1[i]-48; int d2=s2[i]-48; int d3=s3[i]-48;
seq[i]=classification[d1][d2][d3];
}
cout << calculate() << "\n";
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; int classification[10][10][10]; int classify(int a, int b, int c) { if ((a+b)%10==c) { if (a+b<10) return 11; else return 12; } else if ((a+b+1)%10==c) { if (a+b+1<10) return 21; else return 22; } return 0; } int n; int seq[1000005]; LL A[1000005]; LL B[1000005]; LL calculate() { A[n]=0; B[n]=0; int cl; FORD(index,n-1,0) { cl=seq[index]; if (cl==0) { A[index]=0; B[index]=0; } else if (cl==11) { A[index]=1+A[index+1]; B[index]=0; } else if (cl==12) { A[index]=0; B[index]=1+A[index+1]; } else if (cl==21) { A[index]=B[index+1]; B[index]=0; } else if (cl==22) { A[index]=0; B[index]=B[index+1]; } } LL sum=0; FOR(i,0,n-1) sum+=A[i]; return sum; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); FOR(i,0,9) FOR(j,0,9) FOR(k,0,9) classification[i][j][k]=classify(i,j,k); string s1, s2, s3; cin >> s1; cin >> s2; cin >> s3; n=s1.size(); FOR(i,0,n-1) { int d1=s1[i]-48; int d2=s2[i]-48; int d3=s3[i]-48; seq[i]=classification[d1][d2][d3]; } cout << calculate() << "\n"; return 0; } |
English