CodeChef Sereja and LCM(矩阵快速幂)
Sereja and LCMProblem code: SEALCM
|
All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
In this problem Sereja is interested in number of arrays A[1], A[2], ..., A[N] (1 ≤ A[i] ≤ M, A[i] - integer)
such that least common multiple (LCM) of all its elements is divisible by number D.
Please, find sum of answers to the problem with D = L, D = L+1, ..., D = R. As answer could be large, please output it modulo
(109 + 7).
Input
- First line of input contain integer T - number of test cases.
- For each test case, only line of input contain four integers N, M, L, R.
Output
For each test case, output required sum modulo (109 + 7).
Constraints
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 5*106
- 1 ≤ L ≤ R ≤ M ≤ 1000
Subtasks
- Subtask #1: 1 ≤ N, M ≤ 10 (25 points)
- Subtask #2: 1 ≤ N, M ≤ 1000 (25 points)
- Subtask #3: original constraints (50 points)
Example
Input:
2
5 5 1 5
5 5 4 5 Output:
12310
4202
给出 N , M , L , R 。
求 用 1~M ,组成N个数 ,它们的LCM能够整除d的序列个数 , d = L ~ R 。
枚举d , 将d分解 ,d = p1^k1*p2^k2*....*pn^kn .
当一个数与d有相同质因子 pi 且 它的ki 大于等于 d的ki时 ,就能够使得LCM起到作用。
M只有1000 , 直接用二进制表示个个质因子的指数项是否大于等于d 的 ki 。
然后弄好转移矩阵 , 直接快速幂就OK了 。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9+;
const int N = ;
int fac[] , tot , all ; struct Matrix {
LL v[][] ;
Matrix(){ memset( v,,sizeof v); }
Matrix operator * ( const Matrix &a ) const {
Matrix res ;
for( int i = ; i < all ; ++i )
for( int k = ; k < all ; ++k ) {
if( v[i][k] == ) continue ;
for( int j = ; j < all ; ++j ) {
res.v[i][j] = ( res.v[i][j] + v[i][k] * a.v[k][j] % mod )% mod;
}
}
return res;
}
}; Matrix q_pow( Matrix a , int b ) {
Matrix res = a ;
b--;
while( b ) {
if( b& ) res = res * a ;
a = a * a ;
b >>= ;
}
return res ;
} void Work( int d ) {
tot = ;
for( int i = ; i * i <= d ; ++i ) {
if( d % i == ) {
fac[tot++] = ;
while( d % i == ) fac[tot-] *= i , d /= i ;
}
}
if( d > ) fac[tot++] = d ;
} int main() {
// freopen("in.txt","r",stdin);
int _ ; cin >> _ ;
while( _-- ) {
int n , m , l , r ;
LL ans = ;
cin >> n >> m >> l >> r ;
for( int d = l ; d <= r ; ++d ) {
Work(d); Matrix a ;
all = <<tot ;
for( int i = ; i < all ; ++i ) {
for( int j = ; j <= m ; ++j ) {
int st = i ;
for( int k = ; k < tot ; ++k ) {
if( j % fac[k] == ) st |= (<<k) ;
}
a.v[i][st]++;
}
}
a = q_pow(a,n);
ans = ( ans + a.v[][all-] ) % mod ;
}
cout << ans << endl ;
}
}
CodeChef Sereja and LCM(矩阵快速幂)的更多相关文章
- CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)
题目链接 Broken Clock 中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 我们现在 ...
- Codechef Eugene and big number(矩阵快速幂)
题目链接 Eugene and big number 题目转化为 $f(n) = m * f(n - 1) + a$ $f(n + 1) = m * f(n) + a$ 两式相减得 $f(n + 1) ...
- hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x 求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...
- 【BZOJ1898】[ZJOI2005]沼泽鳄鱼(矩阵快速幂,动态规划)
[BZOJ1898][ZJOI2005]沼泽鳄鱼(矩阵快速幂,动态规划) 题面 BZOJ 洛谷 题解 先吐槽,说好了的鳄鱼呢,题面里面全是食人鱼 看到数据范围一眼想到矩乘. 先不考虑食人鱼的问题,直接 ...
- Luogu P4159 [SCOI2009]迷路 矩阵快速幂+精巧转化
大致就是矩阵快速幂吧.. 这个时候会发现这些边权$\le 9$,然后瞬间想到上回一道题:是不是可以建一堆转移矩阵再建一个$lcm(1,2,3,4,5,6,7,8,9)$的矩阵?...后来发现十分的慢q ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- bzoj 1898: [Zjoi2005]Swamp 沼泽鳄鱼【dp+矩阵快速幂】
注意到周期234的lcm只有12,也就是以12为周期,可以走的状态是一样的 所以先预处理出这12个状态的转移矩阵,乘起来,然后矩阵快速幂优化转移k/12次,然后剩下的次数暴力转移即可 #include ...
- CodeChef-----February Challenge 2018---Broken Clock(极坐标+三角函数递推+矩阵快速幂)
链接: https://www.codechef.com/FEB18/problems/BROCLK Broken Clock Problem Code: BROCLK Chef has a clo ...
- E - Recursive sequence HDU - 5950 (矩阵快速幂)
题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...
随机推荐
- NTP时间服务器构建
搭建一个NTP服务器,为整个网络环境中的所有主机提供时间校准服务,具体如下: - 部署一台NTP时间服务器 - 设置时间服务器上层与0.centos.pool.ntp.org同步 - 设置本地服务器层 ...
- 【CF】38E Let's Go Rolling! (dp)
前言 这题还是有点意思的. 题意: 给你 \(n\) (\(n<=3000\)) 个弹珠,它们位于数轴上.给你弹珠的坐标 \(x_i\) 在弹珠 \(i\) 上面花费 \(C_i\) 的钱 可以 ...
- 安装phpredis扩展以及phpRedisAdmin工具
先从phpredis的git拿到最新的源码包:wget https://github.com/nicolasff/phpredis/archive/master.tar.gz 然后解压到进入目录:ta ...
- 《Webkit技术内幕》之页面渲染过程
文章同步到github<Webkit技术内幕>之页面渲染过程 最近拜读了传说中的<Webkit技术内幕>一书,有很大收获,尤其是对页面渲染有了较深的认识.由于功力有限,而且书中 ...
- Mybatis(三)MyBatis 注解方式的基本 用法
在 MyBatis注解 SQL 中,最基本的就是@Select.@Insert.@Update 和@Delete 四种.
- Redis-缓存击穿/穿透/雪崩
缓存击穿/穿透/雪崩 Intro 使用缓存需要了解几个缓存问题,缓存击穿.缓存穿透以及缓存雪崩,需要了解它们产生的原因以及怎么避免,尤其是当你打算设计自己的缓存框架的时候需要考虑如何处理这些问题. 缓 ...
- mysql错误日志及sql日志的区别
my.ini # power by phpStudy 2014 www.phpStudy.net 官网下载最新版 [client] port=3306 [mysql] default-characte ...
- 【2019 Multi-University Training Contest 8】
01: 02: 03:https://www.cnblogs.com/myx12345/p/11655876.html 04: 05: 06:https://www.cnblogs.com/myx12 ...
- Matplotlib系列(四)--plt.bar与plt.barh条形图
(一)竖条条形图 参数说明 参数 说明 类型 x x坐标 int,float height 条形的高度 int,float width 线条的宽度 0~1,默认是0.8 botton 条形的起始位置 ...
- php面试专题---MySQL分区
php面试专题---MySQL分区 一.总结 一句话总结: mysql的分区操作还比较简单,好处是也不用自己动手建表进行分区,和水平分表有点像 1.mysql分区简介? 一个表或索引-->N个物 ...