优秀而强行的十进制快速幂

来源:互联网 发布:解答问题的软件 编辑:程序博客网 时间:2024/05/17 04:27

优秀而强行的十进制快速幂

今天在xehoth大神的带领下,学习了十进制快速幂·····真心强行

 

首先我们先来看看普通的快速幂以及快速乘(已经熟悉快速幂的同学可以跳过本段)

时间复杂度T(n)O(log2n);

空间复杂度S(n)O(n);

 

详解:

快速幂取模算法

所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模()。在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快、计算范围更大的算法,产生了快速幂取模算法

我们先从简单的例子入手:求 a ^ b % c = ? 

算法1.首先直接地来设计这个算法:

int ans = 1;

for (int i = 1; i <= b; i++)

    ans = ans * a;

ans = ans % c;

这个算法的时间复杂度体现在for循环中Ob.这个算法存在着明显的问题如果ab过大很容易就会溢出。

那么,我们先来看看第一个改进方案:在讲这个方案之前,要先有这样一个公式:

ab  mod c = (a mod c)b  mod c这个公式大家在离散数学或者数论当中应该学过,不过这里为了方便大家的阅读,还是给出证明:

引理1

公式:(ab) mod c=[(a mod c) * (b mod c)] mod c

证明:

a mod c = d → a = tc + d

b mod c = e → b = kc + e

ab mod c = (tc + d) (kc + e) mod c

= (tkc+ (te + dk)c + de) mod c

= de mod c = [(a modc) * (b mod c)] mod c

上面公式为下面公式的引理,即积的取余等于取余的积的取余。

公式:ab mod c = (a mod c)b mod c

证明:[(a mod c)b] mod c

= [((a mod c)mod c)b]mod c(由上面公式迭代)

[(a mod c)b]mod c = ab mod c

证明了以上的公式以后,我们可以先让a关于c取余,这样可以大大减少a的大小,

于是不用思考的进行了改进:

算法2

int ans = 1;

a = a % c;//加上这一句

for(int i = 1; i <= b;i++)

    ans = ans * a;

ans = ans % c;

聪明的读者应该可以想到既然某个因子取余之后相乘再取余保持余数不变那么新算得的ans也可以进行取余所以得到比较良好的改进版本。

算法3

int ans = 1;

a = a % c; //加上这一句

for(int i = 1; i <= b; i++)

  ans = (ans * a) % c;//这里再取了一次余

ans = ans % c;

这个算法在时间复杂度上没有改进仍为O(b)不过已经好很多的但是在c过大的条件下还是很有可能超时所以我们推出以下的快速幂算法

快速幂算法依赖于以下明显的公式,我就不证明了。

amod c = ((a2)b/2) mod c , b是偶数

amod c = ((a2)b/2) mod c , b是奇数

有了上述两个公式后,我们可以得出以下的结论:

1.如果b是偶数,我们可以记k = a2 mod c,那么求(k)b/2 mod c就可以了。

2.如果b是奇数,我们也可以记k = a2 mod c,那么求

((k)b/2 mod c * a ) mod c = ((k)b/2 mod c * a) mod c 就可以了。

那么我们可以得到以下算法:

算法4

int ans = 1;

a = a % c;

if (b % 2 == 1)

ans = (ans * a) mod c; //如果是奇数要多求一步可以提前算到ans

k = (a * a) % c;         //我们取a2而不是a

for (int i = 1; i <= b / 2; i++)

    ans = (ans * k) % c;

ans = ans %c;

我们可以看到,我们把时间复杂度变成了O(b / 2).当然,这样子治标不治本。但我们可以看到,当我们令k =(a * a) mod c时,状态已经发生了变化,我们所要求的最终结果即为(k)b/2 mod c而不是原来的ab mod c所以我们发现这个过程是可以迭代下去的。当然,对于奇数的情形会多出一项a mod c,所以为了完成迭代,当b是奇数时,我们通过

ans = (ans * a) % c;来弥补多出来的这一项,此时剩余的部分就可以进行迭代了。

 

形如上式的迭代下去后,当b=0时,所有的因子都已经相乘,算法结束。于是便可以在Olog b的时间内完成了。于是,有了最终的算法:快速幂算法。

算法5:快速幂算法

int ans = 1;

a = a % c;

while (b > 0)

{

if (b % 2 == 1)

ans = (ans * a) % c;

b = b / 2;

a = (a * a) % c;

}

将上述的代码结构化也就是写成函数

#include<iostream>

#include<cstdio>

#include <cmath>

#include <cstring>

#include <algorithm>

using namespace std;

 

int ksm(int a, int b, int c) {

 int ans = 1;

 a = a % c;

   while(b > 0) {

      if (b % 2 == 1)ans = (ans * a) % c;

      b = b / 2;

      a= (a * a) % c;

   }

   returnans;

}

 

int main() {

 int a, b, c;

 freopen("ksm.in", "r", stdin);

 freopen("ksm.out", "w", stdout);

 scanf("%d %d %d",&a, &b, &c);

 printf("%d", ksm(a, b, c));

 return 0; 

}

本算法的时间复杂度为Ologb),能在几乎所有的程序设计(竞赛)过程中通过,是目前最常用的算法之一。

读者可以自行分析,这里我说不多说了,希望本文有助于读者掌握快速幂算法的知识点,当然,要真正的掌握,不多练习是不行的。

 

用法:

快速幂用于a ^b % c的题目,适用于处理大数据,时间复杂度低。至于快速乘应该是这之上的一个延伸(其实我也不知道谁是谁的延伸······),用于计算a *b % c,即当a *b直接爆long  long的恶心情况。

 

Advantages

1、    对数据的包容大,只要所用的mod不超范围即可应用;

2、    直接避开繁琐的高精编写,减小了代码的复杂度。

3、    非递归实现,避免卡常······

Disadvantages

······

 

注意:

1、    一定要记住存储的数据类型,(long  long),一定要记住,否则,出现WA还算好的,一旦出问题,就直接停运······

2、    循环条件是y > 0,不是 >=  0,不要说没提醒过你······

 

适用题目:

病毒

 

题目描述

201511,国际卫生组织公布了一种新型病毒CAI,其复制能力极强,会使人的记忆能力严重衰退。

在每秒内,一个病毒会分身出N个病毒(本体不计),它们和本体拥有着同样的能力,如果N=4,在第一秒初有1个病毒本体,第一秒末分裂出4个,那么第一秒末有5个,它们在第二秒末会再分裂5*4=20个,那么加上最开始的,第二秒末就有25个。

为了抑制这种可怕的病毒,清华大学的医学研究人员经过认真研究这种病毒的基因,发明了一种新型青霉素注射液,能有效的消灭这种病毒。人体只需要注射一次这种青霉素,就可以终身免疫。这种青霉素杀毒的前提是:当病毒的数量必须达到或者超过P个(对人体开始有害),药力才会自动发挥作用,瞬间全部消灭P个。那么,在第M秒末,环境中还有多少病毒呢?(注,第一秒开始就注射了青霉素)

 

输入格式

只有一行,为3个整数NMP;如题目描述(初始时,只有一只病毒)

 

输出格式

只有一行,为第M秒最后剩余的病毒数目


输入样例1

4 3 3

输出样例1

2


输入样例2

100001000 1

输出样例2

0

【样例1说明】

第一秒的病毒分裂出4个,加上本体就是5个,消灭三个还剩两个。

第二秒的病毒分裂出2*4=8个,加上两个本体就是10个,药力发挥3次,消灭了9个,还剩一个。

第三秒剩下的那个分裂出4个,加上本体就是5个,药力发挥一次消灭三个,还剩两个。

【样列2说明】

只要有1个病毒,药力就发挥杀毒功能,显然没有病毒能活下来。

数据规模:

   30%数据:M<=100000

   50%数据:1<=NP<=2^30   1<=M<=1152921504606846976

    100%数据:1<=N<=2^30   1<=M<=11529215046068469761<=P<=2^60

Source

/*created by scarlyw*/#include <cstdio>#include <string>#include <algorithm>#include <cmath>#include <iostream>#include <cstring>#include <cctype>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;}template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return;if (c == '-') iosig = true;}for (x = 0; isdigit(c); c = read())x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}const int OUT_LEN = 1024 * 1024;char obuf[OUT_LEN], *oh = obuf;inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;}template<class T>inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else { if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}}inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }long long a, b;long long x, y, n, m, mod;long long ksc(long long a, long long b) {long long ans = 0;while (b) {if (b & 1) ans = (ans + a) % mod;a = (a + a) % mod, b >>= 1;}return ans;}long long ksm(long long a, long long b) {long long ans = 1;while (b) {if (b & 1)ans = ksc(ans, a);a = ksc(a, a), b >>= 1;}return ans;}void solve() {R(n), R(m), R(mod);std::cout << ksm(n + 1, m);}int main() {solve();return 0;}

Source:(某种科学的O(1)快速乘)

/*created by scarlyw*/#include <cstdio>#include <string>#include <algorithm>#include <cmath>#include <iostream>#include <cstring>#include <cctype>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;}template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return;if (c == '-') iosig = true;}for (x = 0; isdigit(c); c = read())x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}const int OUT_LEN = 1024 * 1024;char obuf[OUT_LEN], *oh = obuf;inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;}template<class T>inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else { if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}}inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }long long a, b;long long x, y, n, m, mod;long long ksc(long long a, long long b) {return (a * b - (long long)((long double)a / mod * b) * mod + mod) % mod;}long long ksm(long long a, long long b) {long long ans = 1;while (b) {if (b & 1)ans = ksc(ans, a);a = ksc(a, a), b >>= 1;}return ans;}void solve() {R(n), R(m), R(mod);std::cout << ksm(n + 1, m);}int main() {solve();return 0;}


只想了解十进制快速幂的dalao们,可以直接从此处开始

思想上没有什么出奇的地方,但是的确非常的优化了ksm的速度,直接上代码比较好

题目背景:

thoj42

Source

/*created by scarlyw*/#include <cstdio>#include <string>#include <algorithm>#include <cmath>#include <iostream>#include <cstring>#include <cctype>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;}template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return;if (c == '-') iosig = true;}for (x = 0; isdigit(c); c = read())x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}const int OUT_LEN = 1024 * 1024;char obuf[OUT_LEN], *oh = obuf;inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;}template<class T>inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else { if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}}inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }const int MAXN = 1000000 + 10;const long long mod = 999999999999999999LL;long long a;char b[MAXN];inline long long mul(long long a, long long b, long long mod) {return (a * b - (long long)((long double)a / mod * b) * mod + mod) % mod;}inline long long mod_pow(long long a, long long b, long long mod) {if (b == 0) return 1;for (; ~b & 1; a = mul(a, a, mod), b >>= 1);/*一个对二进制快速幂的小优化,去除后导零*/long long ret = a;for (b >>= 1; b; b >>= 1)a = mul(a, a, mod), (b & 1) ? ret = mul(ret, a, mod) : 0;return ret;}inline long long mod_pow_solve(long long a, char *b, long long mod) {long long ret = 1;int len = strlen(b);for (int i = len - 1; i >= 0; --i)/*核心思想,每一次直接找当前的位置的pow乘上去,每一次将a ^ (10 ^ n - 1) 变成 a ^ (10 ^ n)*/ ret = mul(ret, mod_pow(a, b[i] ^ '0', mod), mod), a = mod_pow(a, 10, mod);return ret;}int main() {std::ios::sync_with_stdio(false);std::cin.tie(NULL), std::cout.tie(NULL);std::cin >> a >> b;std::cout << mod_pow_solve(a, b, mod);return 0;}

 

0 0
原创粉丝点击