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. gdb安装

    1.卸载原有gdb  以root用户登录  1.1 查询原有gdb包名,执行命令: rpm -q gdb  1.2 卸载原有gdb包,假设gdb包名为gdb-7.0-0.4.16,执行命令:rpm - ...

  2. Delphi 绘图对象

    来自:http://blog.csdn.net/lailai186/article/details/8755430 ========================================== ...

  3. beego学习笔记(2)

    BEEGO的几个特点: 简单化 RESTful 支持.MVC 模型,可以使用 bee 工具快速地开发应用,包括监控代码修改进行热编译.自动化测试代码以及自动化打包部署. 智能化 支持智能路由.智能监控 ...

  4. JavaScript中继承的实现

    继承是类和类之间的关系,继承使得子类别具有父类别的属性和方法. js里常用的如下两种继承方式: 原型链继承(对象间的继承) 类式继承(构造函数间的继承) 由于js不像java那样是真正面向对象的语言, ...

  5. java 获取路径的各种方法

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...

  6. img标签src图片地址找不到显示默认图片

    可以采用onerror的属性: onerror="javascript:this.src='${base}/after/img/aifu.png'" <img id=&quo ...

  7. Codeforces 722C(并查集 + 思维)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5932712.html 题目链接:http://codeforces.com/problemset/problem/722/ ...

  8. Java常用工具类之RegexpUtils,正则表达式工具类

    package com.test.core.util; import org.apache.log4j.Logger; import org.apache.oro.text.regex.Malform ...

  9. FastReport.Net使用:[38]关系的使用

    打印所有成绩 1. 数据源准备 接下来我们需要打印学生成绩,而成绩表中无姓名,我们通过建立Realtion关系来打印数据. 2. 创建Relation关系 在数据视图上的动作下拉菜单中选择“新建关系” ...

  10. 「CTSC2016」单调上升路径

    「CTSC2016」单调上升路径 解题思路:根据提示可以得到答案的下界是 \(n - 1\) ,然后打表发现这个下界好像一定可以取到. 事实上考虑 \(n\) 个点完全图的边数是 \(\frac{n( ...