矩陣快速冪總結
快速冪相關:
//求 mk%pmk%p ,時間複雜度 O(logk)O(logk)。
int qmi(int m, int k, int p)
{
int res = 1, t = m;
while (k)
{
if (k&1) res = res * t % p;
t = t * t % p;
k >>= 1;
}
return res;
}
1.Leetcode 372 Super Pow
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example 1:
Input: a = 2, b = [3]
Output: 8
Example 2:
Input: a = 2, b = [1,0]
Output: 1024
class Solution {
public static final int MOD = 1337;
public int qpow(int a,int n){
int res = 1;
a %= MOD;
while(n != 0){
if((n & 1 )== 1){
res = (res * a) % MOD;
}
a = (a * a) % MOD;
n >>= 1;
}
return res;
}
public int superPow(int a, int[] b) {
int ans = 1, len = b.length;
for (int i = 0; i < len; i ++) {
ans = (qpow(ans, 10) * qpow(a, b[i])) % MOD;
}
return ans;
}
}
2.Leetcode 50 Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−231, 231 − 1]
class Solution {
public:
double myPow(double x, int n) {
double ans = 1;
if(n > 0){
while(n){
if(n & 1){
ans *= x;
}
x *= x;
n >>= 1;
}
}else{
if(n == INT_MIN){
n = INT_MAX;
while(n){
if(n & 1){
ans *= x;
x *= x;
n >>= 1;
}
}
ans = abs(1.0/ans);
}else{
n *= -1;
while(n){
if(n & 1){
ans *= x;
}
x *= x;
n >>= 1;
}
ans = 1.0 / ans;
}
}
return ans;
}
};
3.Acwing 89 a^b
求 aa 的 bb 次方對 pp 取模的值。
輸入格式
三個整數 a,b,pa,b,p ,在同一行用空格隔開。
輸出格式
輸出一個整數,表示a^b mod p
的值。
資料範圍
1≤a,b,p≤1091≤a,b,p≤109
輸入樣例:
3 2 7
輸出樣例:
2
#include <iostream>
using namespace std;
int main(){
int a,b,p;
cin >> a >> b >> p;
int res = 1 % p;
while(b){
if(b & 1){
res = res * 1ll * a % p;
}
a = a * 1ll * a % p;
b >>= 1;
}
cout << res << endl;
return 0;
}
4.Acwing 90 64位整數乘法
求 aa 乘 bb 對 pp 取模的值。
輸入格式
第一行輸入整數aa,第二行輸入整數bb,第三行輸入整數pp。
輸出格式
輸出一個整數,表示a*b mod p
的值。
資料範圍
1≤a,b,p≤10181≤a,b,p≤1018
輸入樣例:
3 4 5
輸出樣例:
2
#include<iostream>
using namespace std;
typedef unsigned long long ull;
int main(){
ull a,b,c;
cin >> a >> b >> c;
ull res = 0;
while(b){
if(b & 1){
res = (a + res) % c;
}
b >>= 1;
a = a * 2 % c;
}
cout << res << endl;
return 0;
}
import java.math.BigInteger;
import java.util.Scanner;
/**
* @Author:HowieLee
* @Date:1/15/2019
* @Description:Acwing
* @version:1.0
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
String c = sc.next();
//String[] b = a.split(" ");
BigInteger a1 = new BigInteger(a);
BigInteger b1 = new BigInteger(b);
BigInteger c1 = new BigInteger(c);
a1 = a1.mod(c1);
b1 = b1.mod(c1);
System.out.println(a1.multiply(b1).mod(c1));
}
}
5.
相關文章
- 矩陣快速冪矩陣
- 矩陣快速冪(快忘了)矩陣
- 【矩陣乘法】【快速冪】遞推矩陣
- HDU 1575 Tr A(矩陣快速冪)矩陣
- HDU 4565 So Easy!(矩陣快速冪)矩陣
- HDU 4686 (推公式+矩陣快速冪)公式矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- P3390 【模板】矩陣快速冪矩陣
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- 從斐波那契到矩陣快速冪矩陣
- HDU 1575 Tr A【矩陣快速冪取模】矩陣
- HDU 1005 Number Sequence:矩陣快速冪矩陣
- HDU5411CRB and Puzzle(矩陣快速冪)矩陣
- HDU3221Brute-force Algorithm(矩陣快速冪&&指數降冪)Go矩陣
- bzoj3240: [Noi2013]矩陣遊戲(矩陣乘法+快速冪)矩陣遊戲
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- BZOJ 3329 Xorequ:數位dp + 矩陣快速冪矩陣
- HDU 4291 A Short problem(矩陣快速冪+迴圈節)矩陣
- UVA 10655 Contemplation! Algebra (矩陣快速冪)矩陣
- POJ 3150 Cellular Automaton(矩陣快速冪)矩陣
- 費馬小定理 + 費馬大定理 + 勾股數的求解 + 快速冪 + 矩陣快速冪 【模板】矩陣
- POJ 2778-DNA Sequence(AC自動機+構建鄰接矩陣+矩陣快速冪)矩陣
- bzoj4887: [Tjoi2017]可樂(矩陣乘法+快速冪)矩陣
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- LightOJ 1070 Algebraic Problem:矩陣快速冪 + 數學推導AI矩陣
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- BZOJ3329: Xorequ(二進位制數位dp 矩陣快速冪)矩陣
- LightOJ 1132 Summing up Powers:矩陣快速冪 + 二項式定理矩陣
- 2015年吉林省賽 Pin Pin Pin(矩陣快速冪)矩陣
- HDU 4549M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- torch中向量、矩陣乘法大總結矩陣
- 向量和矩陣求導公式總結矩陣求導公式
- POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)矩陣
- 斐波那契數列的第N項(1≤n≤10^18 矩陣快速冪)矩陣
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩陣快速冪取模)矩陣