HDU 4669 Mutiples on a circle (2013多校7 1004题)
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
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.
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.
9 6 4 2 8
看了题解发现,题解的做法比我简单多了。
我是先统计没有形成环的,就是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题)的更多相关文章
- HDU 4699 Editor (2013多校10,1004题)
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 4669 Mutiples on a circle 数位DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4669 考察对取模的的理解深不深刻啊,当然还有状态的设计····设d[i][j]表示以第i个数结尾,余 ...
- HDU 4669 Mutiples on a circle(环状DP)
题目链接 这是最早看懂题意的一题,状态转移,挺好想..但是比赛时候,就是没有想到怎么去重,而且当时有些情况,也没注意到. 先预处理的dp[0]的情况,就是以p[0]为结尾的情况.之后D就行了,例如样例 ...
- HDU 4669 Mutiples on a circle (DP , 统计)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个环,每个点是一个数字,取一个子串,使 ...
随机推荐
- julia 1.0如何使用pkg
输入]进入pkg模式 add 加包名即可安装,如 add Cxx
- Linux中常用命令 <一>
本笔记中记录的命令来源于 <Linux C 编程实战> ------------------------------------------------------------------ ...
- bind1st bind2nd的使用
STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...
- python 面试
知识总结 面试(一)
- python爬取网易云音乐歌单音乐
在网易云音乐中第一页歌单的url:http://music.163.com/#/discover/playlist/ 依次第二页:http://music.163.com/#/discover/pla ...
- js中的cookie使用和vue-cookie的使用
在HTTP协议的定义中,采用了一种机制来记录客户端和服务器端交互的信息,这种机制被称为cookie,cookie规范定义了服务器和客户端交互信息的格式.生存期.使用范围.安全性. 在JavaScrip ...
- gtk+学习笔记(三)
gtk感觉函数好多,需要记好多函数,还是多练习,多总结,今天写了一个登陆窗口,很简单,主要是为了加深对这些东西的记忆,直接贴代码 #include<gtk/gtk.h> static Gt ...
- Yii 简明学习教程
Yii是一个基于组件的高性能PHP框架,用于快速开发Web应用程序(下面内容基于Yii 1.1) 1. 典型的工作流 用户向入口脚本index.php发起请求 入口脚本加载应用配置config.php ...
- 【LOJ】#2012. 「SCOI2016」背单词
题解 我们发现第一种操作肯定不可取,每个节点里它最近的点是它最长出现过的后缀,发现这就是AC自动机的fail节点,根据fail的关系这会是一棵树,而一个单词的前一个序号最大的后缀必定是它的父亲 然后我 ...
- cocos2dx各个版本下载地址
https://code.google.com/archive/p/cocos2d-x/downloads?page=1 各种工具包括 NDK 8 https://github.com/fusijie ...