BestCoder Round #62 (div.2) HDOJ5564 Clarke and digits(dp + 快速幂)

原创 2015年11月18日 10:55:40

Clarke and digits

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 88    Accepted Submission(s): 44


Problem Description
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a researcher, did a research on digits. 
He wants to know the number of positive integers which have a length in [l,r] and are divisible by 7 and the sum of any adjacent digits can not be k.
 

Input
The first line contains an integer T(1T5), the number of the test cases. 
Each test case contains three integers l,r,k(1lr109,0k18).
 

Output
Each test case print a line with a number, the answer modulo 109+7.
 

Sample Input
2 1 2 5 2 3 5
 

Sample Output
13 125 Hint: At the first sample there are 13 number $7,21,28,35,42,49,56,63,70,77,84,91,98$ satisfied.
 



题目链接:点击打开链接

dp[i][j]表示长度为i取模为7的种类数, 参考数据范围, 直接计算会TLE, 用矩阵快速幂来加速运算, 第一次做矩阵快速幂的题目, 参考了题

解, 1LL防止数据溢出出错, 转态转移方程: z.dp[i][j] = (z.dp[i][j] + 1LL * a.dp[i][k] * b.dp[k][j] % MOD) % MOD

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 100;
struct Matrix
{
	/* data */
	int row, col, dp[MAXN][MAXN];
	void init(int r, int c) {
		row = r;
		col = c;
		memset(dp, 0, sizeof(dp));
	}
}x, y, z;
int l, r, k;
void magic(int k)
{
	for(int i = 1; i < 10; ++i)
		x.dp[i * 7 + i % 7][0]++;
	for(int i = 0; i < 10; ++i)
		for(int j = 0; j < 7; ++j) {
			int u = i * 7 + j;
			for(int m = 0; m < 10; ++m) 
				if(i + m != k) {
					int v = m * 7 + (j * 10 + m) % 7;
					y.dp[v][u]++;
			}
		}
	for(int i = 0; i <= 10; ++i)
		y.dp[70][i * 7] = 1;
}
void mul(const Matrix &a, const Matrix &b, Matrix &c)
{
	z.init(a.row, b.col);
	for(int i = 0; i < z.row; ++i)
		for(int j = 0; j < z.col; ++j)
			for(int k = 0; k < a.col; ++k)
				z.dp[i][j] = (z.dp[i][j] + 1LL * a.dp[i][k] * b.dp[k][j] % MOD) % MOD;
	c = z;
}
int power_mod(Matrix a, Matrix b, int n)
{
	while(n > 0) {
		if(n & 1) mul(b, a, a);
		mul(b, b, b);
		n >>= 1;
	}
	return a.dp[70][0];
}
int main(int argc, char const *argv[])
{
	int t;
	scanf("%d", &t);
	while(t--) {
		x.init(MAXN, 1);
		y.init(MAXN, MAXN);
		scanf("%d%d%d",&l, &r, &k);
		magic(k);
		int ans = (power_mod(x, y, r) - power_mod(x, y, l - 1) + MOD) % MOD;
		printf("%d\n", ans);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

BestCoder Round #62 (div.2)-Clarke and baton(二分搜索)

Clarke and baton    Accepts: 1    Submissions: 126  Time Limit: 12000/6000 MS (Java/Others)    Memo...

BestCoder Round #62 (div.2)-Clarke and five-pointed star(几何)

Clarke and five-pointed star    Accepts: 237    Submissions: 591  Time Limit: 2000/1000 MS (Java/Ot...

BestCoder Round #62 (div.2) HDU 5563 Clarke and five-pointed star

Clarke and five-pointed star                                           Time Limit: 2000/1000 MS (Ja...

BestCoder Round #62 (div.2)-Clarke and food(模拟)

Clarke and food    Accepts: 353    Submissions: 962  Time Limit: 2000/1000 MS (Java/Others)    Memo...

HDU 5562 Clarke and food(排序)——BestCoder Round #62(div.2)

HDU 5562 Clarke and food(排序)——BestCoder Round #62(div.2)

HDU 5563 Clarke and five-pointed star(判断正五边形)——BestCoder Round #62(div.1 div.2)

HDU 5563 Clarke and five-pointed star(判断正五边形)——BestCoder Round #62(div.1 div.2)

【HDU5565 BestCoder Round 62 (div1)C】【STL or 二分答案 or 计数排序】Clarke and baton n个人减肥m次求最后异或值

【HDU5565 BestCoder Round 62 (div1)C】【优先队列做法】Clarke and baton n个人减肥m次求最后异或值 #include #include #i...

【HDU5563 BestCoder Round 62 (div1)A】【计算几何 凸包】Clarke and five-pointed star 正五边形判定 正五角星判定

#include #include #include #include #include #include #include #include #include #include #include ...

hdu 5564 Clarke and digits (dp+矩阵加速)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 问题描述 克拉克是一名人格分裂患者。某一天,克拉克变成了一个研究人员,在研究数字。 ...
  • w20810
  • w20810
  • 2015年11月16日 13:20
  • 734

BestCoder Round #72 (div.2) B.Clarke and points

& hdoj 5626 题意: 平面上n个点,求两点间最大的曼哈顿距离。 题解: 假设A, B两点使得曼哈顿距离最大,去绝对值,可以化简如下: 则只要分别求出最大和最小的、即可。 #incl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:BestCoder Round #62 (div.2) HDOJ5564 Clarke and digits(dp + 快速幂)
举报原因:
原因补充:

(最多只允许输入30个字)