数论学习1

矩阵快速幂

求矩阵a的k次方:

cpp 复制代码
struct mat{
    ll c[101][101];
    mat(){
        memset(c,0,sizeof(c));
    }
} a, res;

ll n,k;

// 重载运算符,矩阵乘法
mat operator*(mat &x,mat &y){
    mat t;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++){
                t.c[i][j]=(t.c[i][j]+x.c[i][k]*y.c[k][j])%MOD;
            }
        }
    }
    return t;
}

void qpow(ll k){
    for(int i=1;i<=n;i++) res.c[i][i]=1; //单位矩阵
    while(k){
        if(k&1) res=res*a;
        a=a*a;
        k>>=1;
    }
}

void solve(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a.c[i][j];
        }
    }
    qpow(k);

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<res.c[i][j]<<" \n"[j==n];
        }
    }
}

矩阵加速递推

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define i128 __int128_t
#define INF -1e18
#define ls(x) x<<1
#define rs(x) x<<1|1
#define pii pair<int,int>
#define lowbit(x) ((x)&-(x))
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fi first
#define se second
#define endl '\n'
typedef long long ll;
const int N=10001;
const int M=1000000;
const int MOD=1e9+7;
const int maxk=22;

struct mat{
    ll c[3][3];
    mat(){
        memset(c,0,sizeof(c));
    }
}F,A;

mat operator*(mat &a,mat &b){
    mat t;
    for(int i=1;i<=2;i++){
        for(int j=1;j<=2;j++){
            for(int k=1;k<=2;k++){
                t.c[i][j]=(t.c[i][j]+a.c[i][k]*b.c[k][j])%MOD;
            }
        }
    }
    return t;
}

void qpow(int k){
    F.c[1][1]=F.c[1][2]=1;
    A.c[1][1]=A.c[1][2]=A.c[2][1]=1;
    while(k){
        if(k&1) F=F*A;
        A=A*A;
        k>>=1;
    }
}

void solve(){
    int n;
    cin>>n;
    if(n<=2){
        cout<<1<<endl;
        return;
    }
    qpow(n-2);
    cout<<F.c[1][1]<<endl;
}

signed main(){
    IOS;
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }

    return 0;
}

分解质因数

代码:

cpp 复制代码
int n;
int a[10001]; //质因数的个数

//分解质因数
void op(int x){
    for(int i=2;i*i<=x;i++){
        while(x%i==0){
            a[i]++;
            x/=i;
        }
    }
    if(x>1) a[x]++;
}

void solve(){
    cin>>n;
    for(int i=2;i<=n;i++){
        op(i);
    }
    for(int i=1;i<=n;i++){
        if(a[i]){
            cout<<i<<" "<<a[i]<<endl;
        }
    }
}
相关推荐
风落青芜2 小时前
排序算法(四):归并排序、计数排序与算法总结
c语言·开发语言·数据结构·算法·排序算法
@陈小鱼2 小时前
基于InceptionTime网络的PPG无创血压估计算法研究
人工智能·python·算法·回归·开源·ppg·血压估计
老师我太想进步了20268 小时前
【数据结构-15】实现链式结构二叉树
c语言·数据结构
liulilittle9 小时前
认识麻将 —— 流水及对账
算法·游戏·lua·对账·流水·资金·玩法
xiangxueCode11 小时前
数据结构:容器适配器之优先级队列
数据结构·优先级队列
鱼很腾apoc12 小时前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
郝学胜-神的一滴12 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
楚山之石1 天前
【Lancet】BRAID 随机试验:乳腺致密女性补充成像,abbreviated MRI 与 CEM 检出更多早期癌(首轮中期结果)
人工智能·科技·学习·mri·医学影像
青少儿编程课堂1 天前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛