Mutiples on a circle

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 171    Accepted Submission(s): 28

Problem Description
Tom has a necklace with n jewels. There is a number on each jewel. Now Tom wants to select a wonderful chain from the necklace. A chain will be regarded wonderful if the wonderful value of the chain is a multiple of a key number K. Tom gets the wonderful value using this way:He writes down the number on the chain in clockwise order and concatenates them together. In this way, he gets a decimal number which is defined as the wonderful value.
For example, consider a necklace with 5 jewels and corresponding numbers on the jewels are 9 6 4 2 8 (9 and 8 are in neighborhood). Assume we take K=7, then we can find that only five chains can be multiples of K. They are 42, 28, 896, 42896 and 89642.

Now Tom wants to know that how many ways he can follow to select a wonderful chain from his necklace.

 
Input
The input contains several test cases, terminated by EOF.
Each case begins with two integers n( 1 ≤ n ≤ 50000), K(1 ≤ K ≤ 200),the length of the necklace and the key number.
The second line consists of n integer numbers, the i-th number ai(1 ≤ ai ≤ 1000) indicating the number on the ith jewel. It’s given in clockwise order.
 
Output
For each test case, print a number indicating how many ways Tom can follow to select a wonderful chain.
 
Sample Input
5 7
9 6 4 2 8
 
Sample Output
5
 
Source
 
Recommend
zhuyuanchen520
 

看了题解发现,题解的做法比我简单多了。

我是先统计没有形成环的,就是a[n]和a[1]没有连在一起的,这样O(nk)就可以统计完。

然后是统计a[n]和a[1]相连的。

只要把前缀接到后缀的后面的,然后取模统计。

 /* **********************************************
Author : kuangbin
Created Time: 2013/8/13 15:10:49
File Name : F:\2013ACM练习\2013多校7\1004.cpp
*********************************************** */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h> using namespace std; const int MAXN = ;
int a[MAXN]; //第i个数
int end[MAXN];//end[i]表示第i个数...一直连接到第n个数对k取模后的值 int len[MAXN];//第i个数的长度 int b[][]; //滚动数组,预处理以第i个数结尾的,所有连接成的对k取模得到值的个数 int getlen(int n)//得到n有多少位
{
int ret = ;
while(n)
{
ret++;
n/=;
}
return ret;
}
int Ten[];//10^i 预处理,本来预处理了很大10^i的,结果发现一预处理这个就超时,T_T int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,k;
while(scanf("%d%d",&n,&k) == )
{
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
len[i] = getlen(a[i]);
}
Ten[] = ;
for(int i = ;i < ;i++)
Ten[i] = Ten[i-]*%k;
int now = ;
memset(b,,sizeof(b));
b[now][a[]%k] = ;
long long ans = ;
ans += b[now][];
for(int i = ;i <= n;i++)
{
memset(b[now^],,sizeof(b[now^]));
b[now^][a[i]%k] = ;
for(int j = ;j < k;j++)
{
if(b[now][j] == )continue;
int ttt = j*Ten[len[i]]%k+a[i];
ttt%=k;
b[now^][ttt] += b[now][j];
}
now^=;
ans += b[now][]; }
//前面累加的结果是没有a[n]和a[1]连接的。
//后面的是a[n]和a[1]连接的计数
end[n] = a[n]%k;
int tmp = len[n];
int SSSS = Ten[len[n]];
for(int i = n-;i>= ;i--)
{
end[i] = a[i]*SSSS%k + end[i+];
end[i]%=k;
tmp += len[i];
SSSS = SSSS*Ten[len[i]]%k;
}
tmp = len[];
SSSS = Ten[len[]];
int tt = a[]%k;
for(int i = ;i < n;i++)
{
b[now][end[i]]--;
for(int j = ;j < k;j++)
{
int ppp = (j*SSSS%k+tt)%k;
if(ppp == )ans += b[now][j];
}
tt = tt*Ten[len[i+]]+a[i+];
tt%=k;
tmp+=len[i+];
SSSS = SSSS*Ten[len[i+]]%k;
}
printf("%I64d\n",ans);//T_T 一定要long long,这题貌似是刚好超int~~ }
return ;
}

HDU 4669 Mutiples on a circle (2013多校7 1004题)的更多相关文章

  1. HDU 4699 Editor (2013多校10,1004题)

    Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  2. HDU 4679 Terrorist’s destroy (2013多校8 1004题 树形DP)

    Terrorist’s destroy Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  3. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)

    Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  5. HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  6. HDU 4675 GCD of Sequence (2013多校7 1010题 数学题)

    GCD of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  7. HDU 4669 Mutiples on a circle 数位DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4669 考察对取模的的理解深不深刻啊,当然还有状态的设计····设d[i][j]表示以第i个数结尾,余 ...

  8. HDU 4669 Mutiples on a circle(环状DP)

    题目链接 这是最早看懂题意的一题,状态转移,挺好想..但是比赛时候,就是没有想到怎么去重,而且当时有些情况,也没注意到. 先预处理的dp[0]的情况,就是以p[0]为结尾的情况.之后D就行了,例如样例 ...

  9. HDU 4669 Mutiples on a circle (DP , 统计)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个环,每个点是一个数字,取一个子串,使 ...

随机推荐

  1. Spring Boot 在接收上传文件时,文件过大异常处理问题

    Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...

  2. What does “=>” mean in import in scala?(转自StackOverflow问答)

      As others have mentioned, it's an import rename. There is however one further feature that proves ...

  3. Nginx源码分析--数组(转)

    原文地址:http://blog.csdn.net/marcky/article/details/5747431 备注:以下关于Nginx源码的分析基于淘宝开源项目Tengine. Nginx中对数组 ...

  4. ubuntu16.04 安装 python3.6, 并创建虚拟环境(使用python3.6)

    ubuntu16.04 安装 python3.6, 并创建虚拟环境(使用python3.6) ubuntu16.04中默认安装了 python2.7 python3 python3.5.2 (注意 : ...

  5. 杂乱的code

    /*o(n)的堆化方法*/ void myjust(vector<int>& A,int i){ int l=i*2+1; int r=i*2+2; int minn=i; if( ...

  6. 微信小程序-怎么获取当前页面的url

    getCurrentPages() 函数用于获取当前页面栈的实例,以数组形式按栈的顺序给出,第一个元素为首页,最后一个元素为当前页面. https://developers.weixin.qq.com ...

  7. Mysql锁的类型与简析

    数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 根据加锁的范围,MySQL 里面的锁大致 ...

  8. markdown 测试代码

    这是 H1 这是 H2 这是 H3 这是 H4 这是 H5 这是 H6 A First Level Header A Second Level Header Now is the time for a ...

  9. 解决 VUE 微信 IOS 路由跳转问题

    watch: { "$route"(){ if (/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)) { location.hre ...

  10. js过滤检测敏感词汇

    html: <textarea rows="10" cols="100" id="myDiv"></textarea> ...