题目链接:

http://codeforces.com/gym/101161/attachments

题意:

$T$组数据

每组数据包含$L,R,K$

计算$\sum_{k|n}^{}F(n)$

定义$F(n)$为斐波那契数列第$n$项

数据范围:

$1\leq T\leq 10000$

$1\leq L\leq 10^{18}$

$1\leq R\leq 10^{18}$

分析:

博客来源:https://blog.csdn.net/qq_41552508/article/details/97161365

ac代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int maxn = 1e5+100;
const int mod=1e9+7;
struct Node{
ll num[4][4];
Node(){memset(num,0,sizeof(num));}
}dd,ss,tt,gg,zz;
ll dpk,dpk1;
Node mul(Node a,Node b){
Node res;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
for(int k=1;k<=3;k++)
res.num[i][j]=(res.num[i][j]+a.num[i][k]*b.num[k][j]%mod)%mod;
return res;
}
Node qpow(Node x,ll n){
Node res=dd;
while(n>0){
if(n&1)
res=mul(res,x);
x=mul(x,x);
n/=2;
}
return res;
}
ll cal(ll x){
if(x==0)return 0;
Node res=qpow(gg,x-1);
return (dpk*res.num[3][1]%mod+dpk1*res.num[3][2]%mod+dpk*res.num[3][3]%mod)%mod;
}
int main()
{
dd.num[1][1]=dd.num[2][2]=dd.num[3][3]=1;
ss.num[1][1]=ss.num[1][2]=ss.num[2][1]=ss.num[3][1]
=ss.num[3][2]=ss.num[3][3]=1;
tt.num[1][1]=tt.num[1][2]=tt.num[2][1]=tt.num[3][3]=1;
int T;
scanf("%d",&T);
for(int cn=1;cn<=T;cn++){
ll L,R,k;
scanf("%lld %lld %lld",&L,&R,&k);
Node zz=qpow(ss,k-2);
dpk=(zz.num[1][1]*3+zz.num[1][2]*2+zz.num[1][3]*5)%mod;
dpk1=(zz.num[2][1]*3+zz.num[2][2]*2+zz.num[2][3]*5)%mod;
if(k==1)dpk=2,dpk1=1;
gg=mul(ss,qpow(tt,k-1));
printf("Case %d: %lld\n",cn,(cal(R/k)-cal((L-1)/k)+mod)%mod);
}
return 0;
}

  

codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)的更多相关文章

  1. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  2. poj3070矩阵快速幂求斐波那契数列

      Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 9368 Desc ...

  3. 矩阵快速幂--51nod-1242斐波那契数列的第N项

    斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...

  4. UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)

    题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...

  5. 51 Nod 1242 矩阵快速幂求斐波那契数列

    #include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; type ...

  6. 矩阵快速幂 求斐波那契第N项

    #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> us ...

  7. python 快速幂求斐波那契数列

    先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...

  8. Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)

    Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...

  9. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

随机推荐

  1. 数据库优化SQL

    sql优化规则: 1.对于查询,尽量不要使用全表扫描,尽量在where子句以及order by所对应的字段建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放 ...

  2. spring boot logback无感配置

    spring boot1.5.x版本的日志配置一直有一个问题,就是不能直接通过yml配置文件进行日志文件大小进行动态和方便的配置. 怎么解决?直接在springboot项目的maven工程中的src/ ...

  3. JavaScript数组方法之reduce

    又见到数组方法了,在前面已经的多次写到过数组方法,甚至都使用原生方法重构了一遍数组的各个方法,可是随着数组方法reduce的应用,发现reduce真的是妙用无穷啊!还是很值得再拿出来说一遍的. 我们再 ...

  4. hdu 1305 还是字典树

    #include<cstdio> #include<iostream> #include<string> #include<cstdlib> #defi ...

  5. SpringBoot中获取spring.profiles.active的值

    一.网上很多采用@Profile("dev")的方式获取,但是这个是类级别的 二.开发中可能需要代码级别 1.刚开始我想通过classpath下的文件读取方式,麻烦死了,于是换了个 ...

  6. fragment概念理解

    fragment概念理解知识,fragment概念理解图片 fragment概念理解内容,fragment概念理介绍,fragment概念理正文 Fragment是Android honeycomb ...

  7. 轻松使用hsqldb小型数据库

    1.在pom.xml中引入相关jar包 <!-- spring data jpa --> <dependency> <groupId>org.springframe ...

  8. 【转】js中的原型

    原文链接:https://blog.csdn.net/u012468376/article/details/53121081 一.什么是原型原型是Javascript中的继承的基础,JavaScrip ...

  9. datatable 写入excel 2007

    1 添加引用: NPOI NPOI.OOXML 2 private static void GenerateFile(DataTable dt) { DataSet ds = new DataSet( ...

  10. iphone SprintBoard部分私有API总结(不支持iOS8)

    本文介绍iOS SrpintBoard框架的部分私有API,具体包括: 获取ios上当前正在运行的所有App的bundle id(不管当前程序是在前台还是后台都可以) 获取ios上当前前台运行的App ...