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 ...
随机推荐
- Simple GB28181 System
I. Deployment / Architecture Block Diagram II. Resources Used 1. freeswitch —— sip server and media ...
- 【学习】004 java并发包
并发包[jdk1.7] 同步容器类 Vector与ArrayList区别 1.ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不 ...
- python tkinter画圆
x0=150 #圆心横坐标 y0=100 #圆心纵坐标 canvas.create_oval(x0-10,y0-10,x0+10,y0+10) #圆外矩形左上角与右下角坐标 canv ...
- toj 4063 单词(AC自动机)
题目: 小张最近在忙毕设,所以一直在读论文.一篇论文是由许多单词组成的. 但小张发现一个单词会在论文中出现很多次,他想知道每个单词分别在论文中出现了多少次. 输入 第一行一个整数N,表示有N个单词.接 ...
- Git关联JIRA的issue
指导文章 http://www.51testing.com/html/30/n-3724930.html http://{$host_url}/help/user/project/integratio ...
- cocos2dx-Lua3.10版本使用cjson
参考:https://blog.csdn.net/shimazhuge/article/details/79848199 1.首先将cjson加入到libluacocos2d工程(cjson目录:/f ...
- FastDFS搭建文件管理系统
参考:https://www.cnblogs.com/chiangchou/p/fastdfs.html 目录: 一:FastDFS介绍 1:简介: FastDFS 是一个开源的高性能分布式文件系统( ...
- Python 爬虫实战(1):分析豆瓣中最新电影的影评
目标总览 主要做了三件事: 抓取网页数据 清理数据 用词云进行展示 使用的python版本是3.6 一.抓取网页数据 第一步要对网页进行访问,python中使用的是urllib库.代码如下: from ...
- 基于MyBatis实现Dao理论
基于MyBatis实现Dao理论 推荐使用xml提供sql 实现接口推荐使用Mapper自动实现DAO接口,让我们更关注sql书写本身
- [CSP-S模拟测试]:Permutation(线段树+拓扑排序+贪心)
题目描述 你有一个长度为$n$的排列$P$与一个正整数$K$你可以进行如下操作若干次使得排列的字典序尽量小对于两个满足$|i−j|\geqslant K$且$|P_i−P_j|=1$的下标$i$与$j ...