Codeforce Round #213 Div2 C
C. Matrix
time limit per test 1 second
memory limit per test 256 megabytes
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 that x ≤ 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 %I64d specifier.
Sample test(s)
Input
10
12345
Output
6
Input
16
439873893693495623498263984765
Output
40
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <set> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <ctime> 10 #include <algorithm> 11 #include <queue> 12 13 using namespace std; 14 15 #define mp make_pair 16 #define pb push_back 17 #define rep(i,n) for(int i = 0; i < (n); i++) 18 #define re return 19 #define fi first 20 #define se second 21 #define sz(x) ((int) (x).size()) 22 #define all(x) (x).begin(), (x).end() 23 #define sqr(x) ((x) * (x)) 24 #define sqrt(x) sqrt(abs(x)) 25 #define y0 y3487465 26 #define y1 y8687969 27 #define fill(x,y) memset(x,y,sizeof(x)) 28 29 typedef vector<int> vi; 30 typedef long long ll; 31 typedef long double ld; 32 typedef double D; 33 typedef pair<int, int> ii; 34 typedef vector<ii> vii; 35 typedef vector<string> vs; 36 typedef vector<vi> vvi; 37 38 template<class T> T abs(T x) { re x > 0 ? x : -x; } 39 40 const int N = 40000; 41 42 int n; 43 int m; 44 //string s; 45 46 int cnt[N]; 47 char s[4010]; 48 int sum[4010]; 49 int main(){ 50 scanf("%d%s", &n, &s); 51 m = strlen(s); 52 sum[0] = 0; 53 for (int i = 1; i <= m; i++){ sum[i] = sum[i - 1] + s[i - 1] - '0'; } 54 for (int i = 0; i < m; i++){ 55 int cur = 0; 56 for (int j = i; j < m; j++){ 57 cur += s[j] - '0'; 58 cnt[cur]++; 59 } 60 } 61 ll ans = 0; 62 for (int i = 1; i <= m; i++) 63 for (int j = i; j <= m; j++){ 64 if (sum[j] - sum[i - 1] != 0 && n % (sum[j] - sum[i - 1]) == 0 && n / (sum[j] - sum[i - 1])<N) 65 ans += cnt[n / (sum[j] - sum[i - 1])]; 66 else if (n == 0 && sum[j] - sum[i - 1] == 0)ans += (ll) m*(m + 1) / 2; 67 } 68 printf("%I64d\n", ans); 69 return 0; 70 }
浙公网安备 33010602011771号