# hdu4669Mutiples on a circle

http://acm.hdu.edu.cn/showproblem.php?pid=4669

9 6 4 2 8

 1 #include <iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 #define N 50005
7 #define LL __int64
8 int dp[N][210];
9 int b[N],di[N],pp[N*4];
10 int digit(int x)
11 {
12     int i=0;
13     while(x)
14     {
15         i++;
16         x/=10;
17     }
18     return i;
19 }
20 int main()
21 {
22     int i,k,n,r,j;
23     while(scanf("%d%d",&n,&k)!=EOF)
24     {
25         for(i =0 ; i <= n ;i++)
26             for(j = 0;  j < k ; j++)
27             dp[i][j] = 0;
28         for(i = 1; i <= n ; i++)
29         {
30             scanf("%d",&b[i]);
31             di[i] = digit(b[i]);
32         }
33         pp[0] = 1;
34         for(i = 1; i <= n*4 ; i++)
35         pp[i] = (pp[i-1]*10)%k;
36         int pre = 0;
37         b[n+1] = b[1];
38         di[n+1] = di[1];
39         r = 0;
40         for(i = n+1 ; i > 1 ; i--)
41         {
42             r = (b[i]*pp[pre]+r)%k;
43             pre+=di[i];
44             dp[1][r]++;
45         }
46         int rr = r;
47         for(i = 2; i <= n ; i++)
48         {
49             for(r = 0 ; r < k ; r++)
50             {
51                 int o = (r*pp[di[i]]+b[i])%k;
52                 dp[i][o] += dp[i-1][r];
53             }
54             rr = (rr*pp[di[i]]+b[i])%k;
55             dp[i][rr]--;
56             dp[i][b[i]%k]++;
57             rr = (rr-pp[pre]*b[i]+k)%k;
58             if(rr<0) rr+=k;
59         }
60         LL maxz=0;
61         for(i = 1; i <= n ; i++)
62         {
63             maxz+=dp[i][0];
64         }
65         printf("%I64d\n",maxz);
66     }
67     return 0;
68 }
View Code

posted @ 2013-08-14 17:03  _雨  阅读(199)  评论(0编辑  收藏  举报