You have a string of decimal digits s. Let's define bij = si·sj. Find in matrix b the number of such rectangles that the sum bij for all cells(i, j) that are the elements of the rectangle equals a in each rectangle.
A rectangle in a matrix is a group of four integers (x, y, z, t) (x ≤ y, z ≤ t). The elements of the rectangle are all cells (i, j) such thatx ≤ i ≤ y, z ≤ j ≤ t.
Input
The first line contains integer a (0 ≤ a ≤ 109), the second line contains a string of decimal integers s (1 ≤ |s| ≤ 4000).
Output
Print a single integer — the answer to a problem.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Examples
input
10
12345
output
6
input
16
439873893693495623498263984765
output
40
思路:
给你一串数字,只需求出他的子串和(等于给出数字的因子),求出个数。
注意,0的优化。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<iostream> using namespace std; typedef long long LL; LL a[4000+10]; LL b[50005+10]; int main() { string s; int k; cin>>k>>s; int len=s.size(); for(int i=0;i<len;i++) a[i]=s[i]-'0'; for(int i=0;i<len;i++) { int sum=0; for(int j=i;j<len;j++) { sum+=a[j]; b[sum]++; } } LL sum=0; if(k==0) { for(int i=1;i<=50000;i++) sum+=b[i]*b[0]*2; if(b[0]) sum+=b[0]*b[0]; } else { for(int i=1;i<=50000;i++) { if(k%i==0&&b[i]) { int tm=k/i; if(tm>50000) continue; else sum+=b[i]*b[tm]; } } } printf("%lld\n",sum); }