HDU4291 A Short problem

求通項和斐波那契數列的方法一樣,矩陣快速冪。

這道題麻煩在套了三層。

但其實取模這種操作肯定會出現循環的,可以先本地暴出循環節,1000000007對應的循環節是222222224,222222224對應的循環節是183120。

最外層的結果是對1000000007取模,它的內層對222222224取模,可以得到相等的答案,那么222222224的內層對183120取模,也能得到相等的答案,這樣就是分別對三個模數做矩陣快速冪,內層得到的結果返回給外層作為指數。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 typedef __int64 LL;
 5 struct Matrix
 6 {
 7     LL Mt[2][2];
 8     void init0(){memset(Mt, 0, sizeof(Mt));}
 9     void init1() {init0(), Mt[0][0] = Mt[1][1] = 1;}
10     Matrix(){init0();}
11     Matrix(LL num) {init0();Mt[0][0] = Mt[1][1] = num;}
12     Matrix(LL a, LL b, LL c, LL d){Mt[0][0] = a, Mt[0][1] = b, Mt[1][0] = c, Mt[1][1] = d;}
13     Matrix Mul(const Matrix &b, LL mod)
14     {
15         int i, j, k;Matrix res;
16         for(i = 0; i < 2; ++ i)
17             for(j = 0; j < 2; ++ j)
18                 for(k = 0; k < 2; ++ k)
19                     res.Mt[i][j] = (res.Mt[i][j] + Mt[i][k] * b.Mt[k][j]) % mod;
20         return res;
21     }
22     Matrix Rep(LL p, LL mod)
23     {
24         Matrix b = *this, res(1);
25         if(p == 0) return res;
26         if(p == 1) return b;
27         while(p > 1)
28         {
29             if(p & 1) res = res.Mul(b, mod);
30             b = b.Mul(b, mod);
31             p >>= 1;
32         }
33         return b.Mul(res, mod);
34     }
35 };
36 LL Cal(LL n, LL mod)
37 {
38     Matrix mm(3, 1, 1, 0), ori(1, 0, 0, 0);
39     if(!n) return 0;
40     return ori.Mul(mm.Rep(n - 1, mod), mod).Mt[0][0];
41 }
42 int main()
43 {
44     LL n;
45     while(scanf("%I64d", &n) != EOF)
46         printf("%I64d\n", Cal(Cal(Cal(n, 183120), 222222224), 1000000007));
47     return 0;
48 }

轉載于:https://www.cnblogs.com/CSGrandeur/archive/2012/09/16/2687739.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377216.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377216.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377216.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

pvr波形是什么意思_PVR的完整形式是什么?

pvr波形是什么意思PVR&#xff1a;Priya村路演 (PVR: Priya Village Roadshow) PVR is an abbreviation of Priya Village Roadshow. It is one of the biggest and leading multiplex cinema chains in India. PVR是Priya Village Roadshow的縮寫 。 它是印度最大和領先的多元…

《MySQL——查詢長時間不返回的三種原因與查詢慢的原因》

目錄查詢長時間不返回等MDL鎖等flush等行鎖查詢慢構造一張表&#xff0c;表有兩個字段id和c&#xff0c;再里面插入了10萬行記錄 create table t (id int(11) not null,c int(11) default null,primary key (id) ) engine InnoDB;delimiter ;; create procedure idata() begi…

Linux 命令積累 fuser lsof mtr

fuser 用途:使用文件或文件結構識別進程,即:查詢都有哪些進程占用了制定的文件、目錄、設備或套接字;lsof MTR fuser命令 用途:使用文件或文件結構識別進程,即:查詢都有哪些進程占用了制定的文件、目錄、設備或套接字;語法:fuser [-c|-d|-f] [-k] [-u] [-x] [-V] 文件/目錄…

線程終止問題

http://topic.csdn.net/u/20080429/09/9cfe5204-20b5-40fb-ac12-afdc1e4939e9.html?590511460 線程終止問題 http://blog.csdn.net/wuyazhe/article/details/1771470 帶有消息機制的線程 - CustomMessageQueue(c#) using System; using System.Collections.Generic; using Sy…

HTH的完整形式是什么?

HTH&#xff1a;希望這個(那個)有幫助 (HTH: Hope This (That) Helps) HTH is an abbreviation of "Hope This (That) Helps". HTH是“希望有幫助”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking si…

排序算法復習—希爾排序

希爾排序&#xff0c;也稱遞減增量排序算法&#xff0c;是插入排序的一種更高效的改進版本。 希爾排序將整個待排元素序列分割成若干個子序列&#xff08;由相隔某個“增量”的元素組成的&#xff09;分別進行直接插入排序&#xff0c;過程中較小的元素&#xff0c;跳躍式的往前…

《MySQL——幻讀與next-key lock與間隙鎖帶來的死鎖》

create table t (id int(11) not null,c int(11) default null,d int(11) default null,primary key (id),key c (c) ) engine InnoDB;insert into t values(0,0,0),(5,5,5),(10,10,10),(15,15,15),(20,20,20),(25,25,25);該表除了主鍵id&#xff0c;還有索引c。 問下面的語句…

css 陰影 效果_CSS陰影效果

css 陰影 效果CSS中的陰影效果 (Shadow Effects in CSS) It is always good to make our web pages stylish and beautiful, web pages that would catch users eyes instantly but one gets confused as to how to style his or her web page. The confusion is quite legit t…

java常見的ClassNotFoundException-----菜鳥學習java

java常見的ClassNotFoundException 1 - java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 添加包common-logging.jar2 - java.lang.ClassNotFoundException: javax.transaction.Synchronization 添加包jta.jar(hiberante)3 - java.lang.ClassNo…

關于easyui的一些小知識點(1)

讓layout布局自動適應瀏覽器寬度只需要加上fit"true"屬性。轉載于:https://www.cnblogs.com/haifg/p/3613789.html

《MySQL——加鎖規則(待補全,有些沒看懂)》

catalog加鎖規則等值查詢間隙鎖非唯一索引等值鎖主鍵索引范圍鎖非唯一索引范圍鎖唯一索引范圍鎖 bug非唯一索引上存在"等值"的例子limit語句加鎖關于死鎖總結 1、查詢過程中訪問到的對象才會加鎖&#xff0c;而加鎖的基本單位是next-key lock&#xff08;前開后閉&am…

c# 命名空間命名規范_C#中的命名空間

c# 命名空間命名規范C&#xff03;命名空間 (C# Namespace ) In C# namespaces are used to group similar type of classes. Two classes with same name in different namespaces never conflict to each other. 在C&#xff03;中&#xff0c;名稱空間用于對相似類型的類進…

PHP環境搭建:Windows 7下安裝配置PHP+Apache+Mysql環境教程

這兩天剛裝好Windows 7&#xff0c;碰巧前段時間有朋友問我Windows下如何安裝搭建PHP環境&#xff0c;所以打算勤勞下&#xff0c;手動一步步搭建PHP環境&#xff0c;暫且不使用PHP環境搭建軟件了&#xff0c;在此詳細圖解在Windows 7下安裝配置PHPApacheMysql環境的教程&#…

《MySQL—— 業務高峰期的性能問題的緊急處理的手段 》

catalog短連接風暴先處理占著連接但是不工作地線程減少連接過程的消耗慢查詢性能問題索引沒有設計好語句沒寫好選錯索引QPS突增問題短連接風暴 正常的短連接&#xff1a; 執行很少sql語句就斷開&#xff0c;下次需要的時候再重連。MySQL建立連接的過程成本很高&#xff0c;包含…

sql 算出下級銷售總和_找出總和字符串

sql 算出下級銷售總和Description: 描述&#xff1a; This is a standard interview problem to check that the given string is a sum string or not using backtracking. 這是一個標準的面試問題&#xff0c;用于檢查給定的字符串是否為總和字符串或不使用回溯。 Problem…

Request 分別獲取具有相同 name 屬性表單元素值

html 中是允許多個具有相同name屬性的元素的&#xff0c;例如 <div> <input name"txtName" id"txtFirstName" type"text" /> <input name"txtName" id"txtMiddleName" type"text" /> <input…

《MySQL——redo log 與 binlog 寫入機制》

目錄binlog寫入機制redo log寫入機制組提交機制實現大量的TPS理解WAL機制如何提升IO性能瓶頸WAL機制告訴我們&#xff1a;只要redo log與binlog保證持久化到磁盤里&#xff0c;就能確保MySQL異常重啟后&#xff0c;數據可以恢復。 下面主要記錄一下MySQL寫入binlog和redo log的…

BBIAB的完整形式是什么?

BBIAB&#xff1a;再回來一點 (BBIAB: Be Back In A Bit) BBIAB is an abbreviation of "Be Back In A Bit". BBIAB是“ Be Back in A Bit”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites lik…

字符串:KMP Eentend-Kmp 自動機 trie圖 trie樹 后綴樹 后綴數組

涉及到字符串的問題&#xff0c;無外乎這樣一些算法和數據結構&#xff1a;自動機 KMP算法 Extend-KMP 后綴樹 后綴數組 trie樹 trie圖及其應用。當然這些都是比較高級的數據結構和算法&#xff0c;而這里面最常用和最熟悉的大概是kmp&#xff0c;即使如此還是有相當一部分人也…

WPF CanExecuteChanged

繼承ICommand ,RelayCommand命令 1 public class RelayCommand : ICommand2 {3 private readonly Action _execute;4 private readonly Func<bool> _canExecute;5 public event EventHandler CanExecuteChanged;6 public RelayComma…