我把自己演哭了... 心酸.jpg 写了很多个版本的,包括数学公式暴力,快速幂TLE等等,最后想到了优化快速幂里的乘法,因为会爆longlong,但是和别人优化的效率简直是千差万别...? 本题大意: 给定三个longlongint范围内的正整数a, b, c,求出a^b mod c 的结果并输出. 本题思路: 见代码吧. 下面贴出我的各种版本的代码... 参考代码: //这道题数据有点水,不明白为啥数据里1^0 mod 1 == 1 ?魔鬼... /* 数学公式 :: 超时版 #include…
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d      C D   =   c*A+d*C  c*A+d*C 上代码 struct matrix { ll a[maxn][maxn]; }; matrix matrix_mul(matrix x,matrix y) { matrix temp; ;i<=n;i++) ;j<=n;j++) { tem…
hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6217 Accepted Submission(s): 1902 Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] *…
目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分解为(a^2)*(a^2)...a:然后再分别算a^2:这样的计算量由O(n)一下变成 \(O(logn)\): 模板代码如下: ll pow(int a,int b) { if(b==0) return 1; ll res=1 % mod; while(b) { if(b&1) res=res*a…
快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ans=(ans*base)%c; base=(base*base)%c; } return ans; } 快速乘法幂(优化) 幂转换成乘法,乘法转化成加法 public static int f(int a,int b,int c){ int ans = 0; int base=a; while(b!=0…
题目链接: 传送门 A^B mod C Time Limit: 1000MS     Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了,最后又进行了改写. #include<stdio.h> typedef __int64 LL; LL mod_mulit(LL x, LL y,LL mod) { LL res = 0; while (y) { if (y & 1) { res += x; while (res >…
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(ll l,ll r,ll &x,ll &y) { if(r==0){x=1;y=0;return l;} else { ll d=exgcd(r,l%r,y,x); y-=l/r*x; return d; } } 3.求a关于m的乘法逆元 ll mod_inverse(ll a,ll m){ l…
题目信息: 1471: Jiulianhuan 时间限制: 1 Sec  内存限制: 128 MB 提交: 95  解决: 22 题目描述 For each data set in the input print on a separate line, on the standa I think that you might have played the traditional Chinese ring game: The Chinese Linking Rings (here we call…
超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long Pow(long long n,long long m,long long k){//快速幂啊 if(m==1)return n%k; else {long long r=Pow(n,m>>1,k);return (r*r%k)*(m%2?(n%k):1)%k;}//表达式 } int main…
首先复习快速幂 #include<bits/stdc++.h> using namespace std; long long power(long long a,long long b,long long k) { ,base=a; ) { ) { ans*=base; ans%=k; } base*=base; base%=k; b>>=; } return ans; } int main() { long long b,p,k; cin>>b>>p>…