1005 Number Sequence(長得像矩陣快速冪的找規律)
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3 1 2 10 0 0 0Sample Output
2 5
看上去是個矩陣快速冪的題目,實際上由於mod 7導致f(n-1)和f(n-2)只有49種可能的情況,必然會迴圈,找規律即可。
#include<iostream> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; int f(int A,int B,int n) { if(n==1||n==2) return 1; else return (A*f(A,B,n-1)+B*f(A,B,n-2))%7; } int main() { int a,b,n; while(cin>>a>>b>>n,a||b||n) { cout<<f(a,b,n%49)<<endl; } return 0; }
相關推薦
1005 Number Sequence(長得像矩陣快速冪的找規律)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to ca
HDU-6395多校7 Sequence(除法分塊+矩陣快速冪)
review lse %d sca code left define hdu fin Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others
HDU 6198 number number number 矩陣快速冪 找規律
for spa iostream pri amp span ios const isp 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=6198 題目描述: 給你一個k, 你可以用K個斐波那契數列中的數來構造一個數,
Hdu 6198 number number number 矩陣快速冪+找規律
We define a sequence FF : ⋅⋅ F0=0,F1=1F0=0,F1=1 ; ⋅⋅ Fn=Fn−1+Fn−2 (n≥2)Fn=Fn−1+Fn−2 (n≥2) . Give you an integer kk , if a positive number
HDU 1005 Number Sequence(矩陣乘法+快速冪)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
HDU 1005 Number Sequence(基礎矩陣快速冪)
//HDU 1005 15MS 1424K #include <cstdio> #include <cstring> #include <cmath> #in
HDU-1005-Number Sequence (迴圈週期)
原題連結: A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to
1005 Number Sequence(廣義斐波那契數列)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. G
HDU 1005 Number Sequence(找規律,思維)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 153140 Accepted Submissio
hdu 5950 Recursive sequence(矩陣快速冪,構造)
N較大,直接遞推會超時,可以用矩陣快速冪 也是的函式,且 所以在構造的矩陣中維護到 #include <iostream> #include <cstdio> #include <algorithm> #include &
HDU——1005Number Sequence(模版題 二維矩陣快速冪+操作符過載)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 148003 Accepted
BZOJ2553 Beijing2011禁忌(AC自動機+動態規劃+矩陣快速冪+概率期望)
() get lld pac code operator bsp open 自動機 考慮對一個串如何分割能取得最大值。那麽這是一個經典的線段覆蓋問題,顯然每次取右端點盡量靠前的串。於是可以把串放在AC自動機上跑,找到一個合法串後就記錄並跳到根。 然後考慮dp。設f[
BZOJ4000 TJOI2015棋盤(狀壓dp+矩陣快速冪)
狀壓dp div out ons cstring char tdi getchar esp 顯然每一行棋子的某種放法是否合法只與上一行有關,狀壓起來即可。然後n稍微有點大,矩陣快速冪即可。 #include<iostream> #include<c
【HDU2604】Queuing(矩陣快速冪+遞推)
題目連結 Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(
CF989E A Trance of Nightfall(概率+矩陣快速冪優化+倍增)
CF傳送門 洛谷傳送門 【題目分析】 在zxy大佬的講解下終於懂了這道題的做法了qwq。。。 首先根據題意,出發點不一定在特殊點上,但第一次操作後,之後所有的操作都是在特殊點上,所以先考慮從線上出發的最大概率,再加一步即可得到從點出發的最大概率,二者取較大值即可。 記陣列f[i]
DNA Sequence POJ - 2778 AC自動機 矩陣快速冪
題解 給m個長度10以內的病毒串 問長度為n的主串且不匹配任意一個病毒串的有多少個 m最大10所以節點數不超過100 利用AC自動機建圖 建立鄰接矩陣表示從節點i到節點j能轉移的字元數量 除去字元結束節點和fail指標路徑上是結束節點 通過N個鄰接矩陣相乘即可得到i到j走N步的方案數
POJ——3070 Fibonacci (矩陣快速冪求fibonacci)
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n&nbs
HDU 5411 CRB and Puzzle (矩陣快速冪水題)
#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def
Codeforces-954F:Runner's Problem(矩陣快速冪+離散化)
F. Runner's Problemtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are running t
hdu1575(矩陣快速冪入門題)
struct mat{ int m[maxn][maxn]; }unit;//矩陣的資料結構 **過載矩陣*強調內容*乘法** mat operator * (mat a,mat b) { mat ret; ll x;