日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Problem - 3936 FIB Query

發布時間:2024/4/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Problem - 3936 FIB Query 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

We all know the definition of Fibonacci series: fib[i]=fib[i-1]+fib[i-2],fib[1]=1,fib[2]=1.And we define another series P associated with the Fibonacci series: P[i]=fib[4*i-1].Now we will give several queries about P:give two integers L,R, and calculate ∑P[i](L <= i <= R).

Input

There is only one test case.The first line contains single integer Q – the number of queries. (Q<=10^4)Each line next will contain two integer L, R. (1<=L<=R<=10^12)

Output

For each query output one line.Due to the final answer would be so large, please output the answer mod 1000000007.

Sample Input

2
1 300
2 400

Sample Output

838985007
352105429

AC

  • 經過以下推導,只用求單個斐波那契值即可,因為n比較大,用矩陣快速冪

#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <map> #include <bitset> #include <set> #include <string.h> #include <cmath> #include <queue> #include <algorithm> #define N 100005 #define P pair<int,int> #define ll long long #define lowbit(a) a&(-a) #define mk(a, b) make_pair(a, b) #define mem(a, b) memset(a, b, sizeof(a)) ll mod = 1e9 +7; using namespace std; // 矩陣相乘 void mul(ll a[][2], ll b[][2]) {ll ans[2][2];for (int i = 0; i < 2; ++i) {for (int j = 0; j < 2; ++j) {ans[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];}}a[0][0] = ans[0][0] % mod;a[0][1] = ans[0][1] % mod;a[1][0] = ans[1][0] % mod;a[1][1] = ans[1][1] % mod; } ll x[2][2], t[2][2]; // 矩陣快速冪 void quick(ll num) {x[0][0] = 0;x[0][1] = 1;t[0][0] = 0;t[0][1] = 1;t[1][0] = 1;t[1][1] = 1;while (num) {if (num & 1) {mul(x, t);}num >>= 1;mul(t, t);} } int main(){ #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout); #endifint t;scanf("%d", &t);while (t--) {ll l, r;scanf("%lld%lld", &l, &r);l--;ll sumr, suml;quick(2 * r);sumr = x[0][0] * x[0][1] % mod;quick(2 * l);suml = x[0][0] * x[0][1] % mod;ll ans = sumr - suml;printf("%lld\n", (ans + mod) % mod);}#ifndef ONLINE_JUDGEfclose(stdin);fclose(stdout);system("out.txt"); #endifreturn 0; }

總結

以上是生活随笔為你收集整理的Problem - 3936 FIB Query的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。