1. 程式人生 > >hdu 5667 神奇的費馬小定理

hdu 5667 神奇的費馬小定理

題目意思:給出一個遞推公式求出f(n);
這樣的題一般認為是矩陣快速冪,但是這題給出的關係包含冪次,那麼就不能簡單的通過給出的公式來構造矩陣,但是可以通過冪次構造。

這裡寫圖片描述
根據費馬小定理對於a^tp%p,如果a,p互質,那麼a^tp%p = (a^(tp%p-1))%p;
所以矩陣快速速冪的時候要對p-1取餘;
然後最後在求a^z[n]時,要注意a^0 = 0;至於原因對著題目理解一下。

#ifdef __STRICT_ANSI__
#undef __STRICT_ANSI__
#endif
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio> #include<string> #include<cstring> #include<map> #include<queue> #include<set> #include<stack> #include<ctime> #include<algorithm> #include<cmath> #include<vector> #define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; typedef long long ll; typedef long long LL; typedef unsigned long long ull; #define MP make_pair #define PII pair<int,int> #define PFI pair<double,int> #define PLL pair<ll,ll> #define F first #define S second #define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 //freopen("1005.in","r",stdin); //freopen("data.out","w",stdout); typedef vector<ll> Vec; typedef vector<Vec> Mat; const double eps = 1e-7; const double PI = acos(-1.); const double E = 2.71828182845904523536; const int MOD = (int)1e9+7; const int INF = 0x3f3f3f3f; const ll INFLL = 0x7f7f7f7f; const int N = 5e5 + 25; const int M = 1200000 + 5; inline int popcnt(int x){return __builtin_popcount(x); } inline int clz(int x){return __builtin_clz(x);} //末尾的 0,即對 lowbit 取log inline int clz(LL x){return __builtin_clzll(x);} inline int lg2(int x){ return !x ? -1 : 31-clz(x);} inline int lg2(LL x){ return !x ? -1 : 63-clz(x);} int T; Mat I; ll n,a,b,c,p,MYMOD; Mat mul(Mat a,Mat b){ Mat ret(a.size(),vector<ll>(b[0].size(),0)); for(int i=0;i<a.size();i++){ for(int j=0;j<b[0].size();j++){ for(int k=0;k<a[0].size();k++){ ret[i][j] = (ret[i][j] + (a[i][k] * b[k][j]) % MYMOD) % MYMOD; } } } return ret; } Mat quick_mp(Mat mat,ll b){ Mat ret = I; while(b){ if(b&1) ret = mul(ret,mat); b >>= 1; mat = mul(mat,mat); } return ret; } ll quick(ll a,ll b,ll MOD){ if(a ==0 ||b == 0)return 0; ll ret = 1; while(b){ if(b&1) ret = ret * a % MOD; a = a * a % MOD; b >>= 1; } return ret; } int main(){ scanf("%d",&T); while(T --) { cin >> n >> a >> b >> c >> p; if(n == 1){ puts("1"); continue; } Mat base(3,vector<ll>(3,0)); base[0][1] = base[1][0] = base[2][2] = 1; base[0][0] = c; base[0][2] = b; Mat mat(3,vector<ll>(1,b)); mat[1][0] = 0; mat[2][0] = 1; I = Mat(3,vector<ll>(3,0)); // init初始化 for(int i = 0; i < 3; i ++) I[i][i] = 1; ll ans; if(n == 2){ ans = quick(a,b,p) % p; }else{ MYMOD = p - 1; base = quick_mp(base,n-2); mat = mul(base,mat); ll tp = mat[0][0]; ans = quick(a,tp,p) % p; // 快速冪 } cout << ans << endl; } return 0; }

相關推薦

HDU - 1576(定理求逆元)

math src typedef pow ble inpu show font type 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Othe

HDU 6400(定理

傳送門 題面: Freshmen frequently make an error in computing the power of a sum of real numbers, which usually origins from an incorrect equat

HDU 4704 Sum(定理,組合數學,快速冪)

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Sub

[HDU 4704] Sum · 定理 & 快速冪

題意:給定n,設是將n分成k個數之和的方案數,求 隔板原理:將n個物品分成k組,相當於在n-1個間隔中插入k-1個隔板,方案數為,所以等於,貌似是叫二項式定理來著?反正這個式子的值等於,所以就是要求的

hdu 5525 Product (定理優化的快速冪)

@(K ACMer) 題意: 給你一個n個數的數列:an. 求∏i=1niai%mod 分析: 知識補充: - 一個數的約數的個數等於:(p1+1)∗(p2+1)∗....∗(pn+1)(pi表示這個數的各個質因子的個數)

hdu 4704 Sum (定理+快速冪)

//(2^n-1)%mod //費馬小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) # include <stdio.h> # include <algorithm> # include &l

HDU 4704 Sum 定理+快速冪

Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 18    Accepted Submission

hdu 5667 神奇定理

題目意思:給出一個遞推公式求出f(n); 這樣的題一般認為是矩陣快速冪,但是這題給出的關係包含冪次,那麼就不能簡單的通過給出的公式來構造矩陣,但是可以通過冪次構造。 根據費馬小定理對於a^tp%p,如果a,p互質,那麼a^tp%p = (

hdu 5667 Sequence(矩陣快速冪+定理+快速冪)

#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #inc

HDU 5667 Sequence 矩陣快速冪 + 定理

olion August will eat every thing he has found.     Now there are many foods,but he does not want to eat all of them at once,so he find a

HDU 5667 Sequence【矩陣快速冪+定理

題目連結: 題意: Lcomyn 是個很厲害的選手,除了喜歡寫17kb+的程式碼題,偶爾還會寫數學題.他找到了一個數列: fn=1,ab,abfcn−1fn−2,n=1n=2otherwi

HDU 5667 Sequence (矩陣快速冪+定理)

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2687    Acce

HDU 5667 Sequence(矩陣快速冪+定理)

大意: He gives you 5 numbers n,a,b,c,p,and he will eat fn foods.But there are only p foods,so you should tell him fn mod p. Inpu

HDU 4704 Sum(隔板原理+組合數求和公式+定理+快速冪)

ace php 模板 erl char printf 證明 style ron 題目傳送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sam

HDU 5201 The Monkey King(組合數學)(隔板法+容斥定理+定理)

逆元 cst 大於 ont amp space pro http strong http://acm.hdu.edu.cn/showproblem.php?pid=5201題意:給你n個桃子要你分給m只猴子,猴子可以得0個桃子,問有多少種方法,但是有一個限制條件: 第一只猴

hdu 3037 定理+逆元求組合數+Lucas定理

void log 打表 數學 mod turn ret iostream toc 組合數學推推推最後,推得要求C(n+m,m)%p 其中n,m小於10^9,p小於1^5 用Lucas定理求(Lucas定理求nm較大時的組合數) 因為p數據較小可以直接階乘打表求逆元

題解報告:hdu 6440 Dream(定理+構造)

sin hdu 給定 集合 代碼 \n png mes 乘法 解題思路:給定素數p,定義p內封閉的加法和乘法運算(運算封閉的定義:若從某個非空數集中任選兩個元素(同一元素可重復選出),選出的這兩個元素通過某種(或幾種)運算後的得數仍是該數集中的元素,那麽,就說該集合對於

HDU 3037 Saving Beans (隔板法 Lucas定理 定理 乘法逆元)

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7780&nbs

Sum HDU 4704(數論+定理+找規律)

分類:數論+費馬小定理+找規律 題意:給出N,s(k)=num(x1,x2,x3...,xk).s(k)==將N分成k組得數量。給出N問(s(1)+s(2)....s(N))%mod得值。 思路:看懂題意手動模擬算以下前面幾個= = N=1 s(1)=1 ans=1

hdu 4704 Sum (整數和分解+快速冪+定理降冪)

題意: 給n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7)。其中si表示n由i個數相加而成的種數,如n=4,則s1=1,s2=3。                         (全題文末) 知識點: 整數n有種和分解方