-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codeforces-364A - Matrix .cpp
68 lines (58 loc) · 2.1 KB
/
Codeforces-364A - Matrix .cpp
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
#include<bits/stdc++.h>
using namespace std;
// After some observation it can be said that if (i1,j1) is top left corner co-ordinate and right bottom co-ordinate is (i2,j2)
// Then sum of elements is within this rectangle is sum(i1,j1)*sum(i2,j2) ->where sum(i,j)=si+....+sj .
// We have to found out the sum of total possible segment of given string S.
// Then the given sum A is obviously in form of sum(i1,j1)*sum(i2,j2)=X*Y. So , obviously X and Y is a factor of A
// We have to find out the frequency of factors of A in sum of all segments of S.
// For A=0.Then X*Y=0. So , think about non_zero and zero sum segments.it will be 2*#include<bits/stdc++.h>
using namespace std;
// After some observation it can be said that if (i1,j1) is top left corner co-ordinate and right bottom co-ordinate is (i2,j2)
// Then sum of elements is within this rectangle is sum(i1,j1)*sum(i2,j2) ->where sum(i,j)=si+....+sj .
// We have to found out the sum of total possible segment of given string S.
// Then the given sum A is obviously in form of sum(i1,j1)*sum(i2,j2)=X*Y. So , obviously X and Y is a factor of A
// We have to find out the frequency of factors of A in sum of all segments of S.
// For A=0.Then X*Y=0. So , think about zero_segments and total_segments .
typedef unsigned long long ll;
int main()
{
ios::sync_with_stdio(false);
ll A;
cin>>A;
string s;
cin>>s;
ll n=(ll)s.size();
map<ll,ll>freq;
ll zero_segments=0ll;
ll tot_segments=n*(n+1)/2ll;
for(int i=0;i<s.size();i++)
{
ll sum=0ll;
for(int j=i;j<s.size();j++)
{
sum+=(ll)s[j]-'0';
if(sum==0ll) zero_segments++;
freq[sum]++;
}
}
ll res=0ll;
if(A==0ll)
{
res=2ll*(zero_segments*tot_segments) -(zero_segments*zero_segments);
}
else
{
for(ll i=1;i*i<=A;i++)
{
if(A%i==0)
{
if(i*i==A)
{
res+=(1ll*freq[i]*freq[i]);
}
else res+=(2ll*freq[i]*freq[A/i]);
}
}
}
cout<<res<<endl;
}