zoukankan      html  css  js  c++  java
  • 快速幂算法

    快速幂,是为了解决形如(n^m)的结果的算法
    暴力求它求要(m)次运算
    但运用快速幂就可以简化为接近(log(m))的时间复杂度
    一般来说快速幂会在取模的意义下进行运算
    所以pow就会受限,我们需要快速幂来救
    (下面的代码就不带取模了啊~)
    快速幂有两种求法,方法相近
    分别是(while)和递归的做法:
    (while)写法:

    int ksm(int x,int y){
    	int tot=0,tmp=x;
    	while(y){
    		if(y%2==1)tot*=tmp;
    		tmp*=tmp;
    		y/=2;
    	}
    	return tot;
    }
    

    递归写法:

    int ksm(int x,int y){
    	if(y==1) return x;
    	int tmp=ksm(x,y/2);
    	tmp*=tmp;
    	if(y&1) tmp*=x;
    	return tmp;
    }
    

    这是一种算法,在很多数论题中会用到
    可以说,这是数论题的代码实现的重要基础
    所以说,(why) (not)记住它?

    Reality&Imagine
  • 相关阅读:
    索引压缩
    拼写校正
    词典(词汇表)
    Text Relatives II
    Text Relatives
    CoreText
    Quartz2D Text
    PDF Document Creation, Viewing
    Core Graphics Layer Drawing
    Bitmap Images and Image Masks
  • 原文地址:https://www.cnblogs.com/yang-RA-NOI/p/13435585.html
Copyright © 2011-2022 走看看