一位同学的笔记:
链接: 同学.
二分:
单调线性的根据端点查找元素
链接: link.
#include<bits/stdc++.h>
using namespace std;
#define long long ll;
int main(){
vector<int> v;
for(int i=0;i<10;i++){
v.push_back(i);
}
int i=lower_bound(v.begin(),v.end(),5)-v.begin();
cout<<i<<endl;
system("pause");
}
结果:5
精度:
double l,r,mid;
while(right - left > eps)1e-8
mid = (right + left)/2;
if(judge(mid)) left = mid;
else right = mid;
return mid;
三分:
快速冥
(a^b)mod p
#include<bits/stdc++.h>
using namespace std;
#define long long ll;
int quickpow(int a,int p,int mod){
a=a%mod;
int ans=1;
while(p){
if(p&1) ans=(ans*a)%mod;
p=p>>1;
a=(a*a)%mod;
}
return ans;
}
int main(){
int a,b,c;
cin>>a>>b>>c;
cout<<quickpow(a,b,c)<<endl;
system("pause");
return 0;
}
矩阵快速幂
线性递推式
快速冥的底数变成矩阵
以logbde优势线性递推
#include <stdio.h>
#include <iostream>
#include<string.h>
using namespace std;
struct node {
int mat[2][2];
};
int mod = 10000;
int len=2;
struct node mul(struct node x,struct node y){
struct node tmp;
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
tmp.mat[i][j]=0;
for(int k=0;k<len;k++){
tmp.mat[i][j]+=(x.mat[i][k]*y.mat[k][j])%mod;
}
tmp.mat[i][j]=tmp.mat[i][j]%mod;
}
}
return tmp;
}
struct node matpow(struct node x,struct node y,int num){
while(num){
if(num&1){
y=mul(y,x);
}
x=mul(x,x);
num=num>>1;
}
return y;
}
char res[2][2];
int main()
{
int N;
while(scanf("%d",&N),N!=-1)
{
struct node a,b,res;
a.mat[0][0]=1;a.mat[0][1]=1;a.mat[1][0]=1;a.mat[1][1]=0;
b.mat[0][0]=1;b.mat[0][1]=1;b.mat[1][0]=1;b.mat[1][1]=0;
if(N==0)
{
printf("0\n");
continue;
}
if(N==1)
{
printf("1\n");
continue;
}
else
{
res=matpow(a,b,N-2);
printf("%d\n",res.mat[0][0]);
}
}
system("pause");
return 0;
}