标签:
#include <stdio.h> #include <string.h> #define mod 100000007 typedef __int64 LL; int n, s, a, b, ret, a1, a_1; void DFS(LL tmp, LL sum, int k) { if (k == n) { if (sum == s && ++ret == mod) ret -= mod; return; } // time to cut LL a0 = tmp + a; // 如果剩下所有项都+a结果还比s小 int n1 = n - k; LL sum0 = n1 * a0 + n1 * (n1 - 1) * a / 2 + sum; if (sum0 < s) return; a0 = tmp - b; // ...还比s大 sum0 = n1 * a0 - n1 * (n1 - 1) * b / 2 + sum; if (sum0 > s) return; DFS(tmp + a, sum + tmp + a, k + 1); DFS(tmp - b, sum + tmp - b, k + 1); } int main() { int i; scanf("%d%d%d%d", &n, &s, &a, &b); a1 = (s - n * (n - 1) * a / 2) / n; a_1 = (s - n * (n - 1) * (-b) / 2) / n; for (i = a1; i <= a_1; ++i) DFS(i, i, 1); printf("%d\n", ret); return 0; }
标签:
原文地址:http://blog.csdn.net/chang_mu/article/details/42581777