5 7 9 6 4 2 8
5
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> typedef long long ll; using namespace std; const int maxn = 50010; const int mod = 210; int n, k; int dp[maxn][305], num[maxn<<1]; int len[maxn<<1], doMod[maxn<<2]; int digit(int a) { int len = 0; while (a) { a /= 10; len++; } return len; } void getMod(int k, int n) { doMod[0] = 1; for (int i = 1; i <= n<<2; i++) doMod[i] = (doMod[i-1] * 10) % k; } int main() { int n, k; while (scanf("%d%d", &n, &k) != EOF) { for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); num[i+n] = num[i]; } getMod(k, n); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = 0; int r; for (int i = 1; i <= n; i++) { len[i+n] = len[i] = digit(num[i]); r = num[i] = num[i+n] = num[i] % k; dp[i][r]++; } r = num[1]; int sunlen = 0; for (int i = n; i > 1; i--) { sunlen += len[i+1]; r = (num[i] * doMod[sunlen] + r) % k; dp[1][r]++; } sunlen = 0; for (int i = 1; i <= n; i++) sunlen += len[i]; int last = r; ll ans = dp[1][0]; for (int i = 2; i <= n; i++) { for (int j = 0; j < k; j++) { r = (j * doMod[len[i]] + num[i]) % k; dp[i][r] += dp[i-1][j]; } last = (last * doMod[len[i]] + num[i]) % k; dp[i][last]--; last = (last - (num[i] * doMod[sunlen]) % k + k) % k; ans += dp[i][0]; } printf("%lld\n", ans); } return 0; }
HDU - 4669 Mutiples on a circle
原文:http://blog.csdn.net/u011345136/article/details/41019127