Sereja and LCM

 
Problem 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

  • 1T10
  • 1N5*106
  • 1LRM1000

Subtasks

  • Subtask #1: 1N, M10 (25 points)
  • Subtask #2: 1N, 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(矩阵快速幂)的更多相关文章

  1. CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)

    题目链接  Broken Clock   中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 我们现在 ...

  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) ...

  3. hdu 5451 Best Solver 矩阵循环群+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x    求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...

  4. 【BZOJ1898】[ZJOI2005]沼泽鳄鱼(矩阵快速幂,动态规划)

    [BZOJ1898][ZJOI2005]沼泽鳄鱼(矩阵快速幂,动态规划) 题面 BZOJ 洛谷 题解 先吐槽,说好了的鳄鱼呢,题面里面全是食人鱼 看到数据范围一眼想到矩乘. 先不考虑食人鱼的问题,直接 ...

  5. Luogu P4159 [SCOI2009]迷路 矩阵快速幂+精巧转化

    大致就是矩阵快速幂吧.. 这个时候会发现这些边权$\le 9$,然后瞬间想到上回一道题:是不是可以建一堆转移矩阵再建一个$lcm(1,2,3,4,5,6,7,8,9)$的矩阵?...后来发现十分的慢q ...

  6. 5950 Recursive sequence (矩阵快速幂)

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

  7. bzoj 1898: [Zjoi2005]Swamp 沼泽鳄鱼【dp+矩阵快速幂】

    注意到周期234的lcm只有12,也就是以12为周期,可以走的状态是一样的 所以先预处理出这12个状态的转移矩阵,乘起来,然后矩阵快速幂优化转移k/12次,然后剩下的次数暴力转移即可 #include ...

  8. CodeChef-----February Challenge 2018---Broken Clock(极坐标+三角函数递推+矩阵快速幂)

    链接:  https://www.codechef.com/FEB18/problems/BROCLK Broken Clock Problem Code: BROCLK Chef has a clo ...

  9. E - Recursive sequence HDU - 5950 (矩阵快速幂)

    题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...

随机推荐

  1. SCRUM REPORT DIRECTORY

    Alpha sprint scrum 1 scrum 2 scrum 3 scrum 4 scrum 5 scrum 6 scrum 7 scrum 8 scrum 9 scrum 10 Beta s ...

  2. Python之列表、元组、字典、集合及字符串的详细使用

    1.列表 列表相当与C++中的数组,是有序的项目, 通过索引进行查找,但使用起来却方便很多,具体的操作看代码,自己实践一次就非常简单了. 注:列表一般用中括号“[ ]” #列表(数组) name_li ...

  3. zabbix邮件报警通过脚本来发送邮件

    zabbix默认邮件报警会将各个报警接收人单独发送邮件,为了使邮件能以群发的方式统一一封邮件发送所有接收人,需要改成脚本的形式: sendemail.py: #!/usr/bin/python imp ...

  4. [STL]lower_bound&upper_bound

    源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...

  5. 回归平方和 ESS,残差平方和 RSS,总体平方和 TSS

    https://zhidao.baidu.com/question/565190261749684764.html 回归平方和 ESS,残差平方和 RSS,总体平方和 TSS   总变差       ...

  6. .net core跨平台

    https://www.cnblogs.com/artech/p/7812811.html .net简介:https://baike.baidu.com/item/.NET/156737?fr=ala ...

  7. vue双向数据绑定对于数组和新增对象属性不能监听的解决办法

    出现数组不能按照索引进行跟新的原因是处于性能考虑的,但是整体数组的增加删除是可以监听到的:对于对象新增属性不能监听是因为没有在生成vue实例时候放进watcher收集依赖. 首先我们先来了解vue数据 ...

  8. 2019 上海网络赛 J stone name (01背包)

    题目:https://nanti.jisuanke.com/t/41420 题意:给你一个集合,然后让你拆成两个集合 x,y    求满足  x>y  &&  x-(x集合中最小 ...

  9. VMware Workstation 官方正式版及激活密钥

    热门虚拟机软件VMware Workstation Pro现已更新至14.1.2,14.0主要更新了诸多客户机操作系统版本,此外全面兼容Wind10创建者更新.12.0之后属于大型更新,专门为Win1 ...

  10. python3爬虫开发与实战预览版

    https://germey.gitbooks.io/python3webspider/content/1.2.3-ChromeDriver%E7%9A%84%E5%AE%89%E8%A3%85.ht ...