yp模板p7二分三分矩阵快速冥


一位同学的笔记:
链接: 同学.

二分:

单调线性的根据端点查找元素
链接: 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;

三分:

求非线性最值
链接: 3.
链接: 2+3.

快速冥

(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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.0-0.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值