-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1573.cpp
More file actions
49 lines (49 loc) · 1.3 KB
/
Copy path1573.cpp
File metadata and controls
49 lines (49 loc) · 1.3 KB
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
const int mod = 1e9 + 7;
class Solution {
public:
int mod_mul(int x, int y){
int res = 0;
for(int i=0; i<x; i++){
res = res%mod + y%mod;
res %= mod;
}
return res%mod;
}
int numWays(string s) {
int count_one = 0, n = s.size();
for(int i=0; i<n; i++)
if(s[i]=='1')
count_one++;
if(count_one==0){
long long int res = (((long long)(n-1))*((long long)(n-2)))/2;
return res%mod;
}
if(count_one%3 != 0)
return 0;
int x = count_one/3;
vector<pair<int,int>> res(3,{-1,-1});
int j = 0;
res[j].first = 0;
int c = 0;
for(int i=0; i<n; i++){
if(s[i]=='1'){
x--;
if(res[j].second == -1)
res[j].second = i;
}
if(x==0){
c++;
if(c==3)
break;
x = count_one/3;
j++;
res[j].first = i+1;
}
}
// for(int i=0; i<3; i++){
// cout<<res[i].first << " " << res[i].second << "\n";
// }
int y = mod_mul(res[1].second-res[1].first + 1,res[2].second-res[2].first + 1);
return y;
}
};