HDU 4669 Mutiples on a circle (2013多校7 1004题)

时间:2023-02-25 13:31:15

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&comma;1004题)

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

  2. HDU 4679 Terrorist’s destroy &lpar;2013多校8 1004题 树形DP&rpar;

    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题 二分匹配&plus;强连通)

    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&lpar;环状DP&rpar;

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

  9. HDU 4669 Mutiples on a circle (DP &comma; 统计)

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

随机推荐

  1. Chrome Extension 检查视图(无效)处理方法

    最近闲来无事,简单看了下Chrome扩展的开发,并且开发一个小小的翻译插件(TranslateBao)作为练手,开发细节不详述了,如果有新学习chrome extension开发的新人,可以参考源码, ...

  2. 20160113 js中选择多个check一块删除

    js中<script type="text/javascript"> $(document).ready(function (e) { $("#Button2 ...

  3. 使用saripaar对android输入控件进行快速验证

    saripaar是个android的第三方快速校验,使用注解快速添加验证规则. public class LoginActivity extends Activity implements Valid ...

  4. ocument的createDocumentFragment&lpar;&rpar;方法

    在<javascript高级程序设计>一书的6.3.5:创建和操作节点一节中,介绍了几种动态创建html节点的方法,其中有以下几种常见方法: · crateAttribute(name): ...

  5. redhat下升级gcc编译器

    在有网络的环境下,采用下载gcc源码进行编译的方式升级gcc版本,所以需要本身已有gcc编译器. 获取 gcc-4.9.2的包: wget http://gcc.skazkaforyou.com/re ...

  6. iOS 使用SBJSON创建和解析JSON

    原文地址:http://blog.csdn.net/gf771115/article/details/7718403 //创建JSON NSDictionary *dictonary = [[NSMu ...

  7. ggplot2 scale相关设置2—时间设置

    在scale设置中,常用的日期方面的设置函数包括: scale_x_date(),scale_y_date(),scale_x_datetime(),scale_y_datetime()   接下来, ...

  8. RBM如何训练?

    RBM(Restricted Boltzman Machine,受限玻尔兹曼机)是深度学习的基础,虽然原理比较简单,但实际训练中用到了很多trick,在参考文献中,Hinton为我们披露了几个训练的细 ...

  9. webstorm 2018 激活破解方法大全

    转载自:https://blog.csdn.net/voke_/article/details/76418116 方法一:(更新时间:2018/4/8)v3.3 注册时,在打开的License Act ...

  10. Linux常用指令笔记

    目标:统计当前目录下java文件的个数 指令:`ls -R ./ | grep .java$ | wc -l` 原理:`ls -R ./`列出当前文件夹下的所有FILE,包括目录以及文件;`grep ...