2019.7.30 素数筛、矩阵快速幂 题解

本文深入探讨了素数查询的高效算法,包括易用的欧拉筛法,并通过矩阵运算解决斐波那契数列及更复杂的数学问题。文章还提供了解决特定数学难题的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A: 素数查询(Easy)

一个欧拉筛然后查询完事

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 2e5 + 7;

int isprime[MAXN]; //保存素数
int vis[MAXN];    //初始化
void eulerSieve()
{
    int cnt = 0;
    memset(vis, 0, sizeof(vis));  //0是素数,1是合数
    for (int i = 2; i < MAXN; i++){
        if (!vis[i])
            isprime[cnt++] = i;
        for (int j = 0; j < cnt && i * isprime[j] < MAXN; j++){
            vis[i * isprime[j]] = 1;
            if (i % isprime[j] == 0){
                break;
            }
        }
    }
}

int main()
{
    eulerSieve();
    int a;
    while(cin >> a && a){
        if(!vis[a]){
            cout << "Yes" << endl;
        }
        else cout << "No" << endl;
    }
    return 0;
}

 

B: 素数查询(Hard)

还是欧拉筛,可以把每个数(包括这个数)前面的素数个数使用一个前缀素数个数数组 pre[ ] 存下来,求 [ a, b ] 区间素数个数时,只需访问 pre[b] - pre[a-1] 即可

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 2e5 + 7;
 
int isprime[MAXN]; //保存素数
int vis[MAXN];    //打表
int pre[MAXN];    //前缀素数个数
void eulerSieve()
{
    int cnt = 0;
    memset(vis, 0, sizeof(vis));  //0是素数,1是合数
    for (int i = 2; i < MAXN; i++){
        if (!vis[i])
            isprime[cnt++] = i;
        pre[i] = cnt;  //存下前缀素数个数
        for (int j = 0; j < cnt && i * isprime[j] < MAXN; j++){
            vis[i * isprime[j]] = 1;
            if (i % isprime[j] == 0){
                break;
            }
        }
    }
}
 
int main()
{
    eulerSieve();
    int T, a, b;
    scanf("%d", &T);
    while(T--){
        scanf("%d %d", &a, &b);
        cout << pre[b] - pre[a-1] << endl;
    }
    return 0;
}

 

C: 求斐波那契数列

构造出转移矩阵 $$ \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \tag{4} $$ ,我们可以通过这个转移矩阵让初始矩阵 $$ \begin{bmatrix} f(2) & f(1) \\ f(1) & f(0) \end{bmatrix} \tag{4} $$ 变成 $$ \begin{bmatrix} f(n+1) & f(n) \\ f(n) & f(n-1) \end{bmatrix} \tag{4} $$

而我们的初始矩阵值也是 $$ \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \tag{4} $$,所以这个题实际上就是求 $$ \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \tag{4} $$^{n}。由于数据较大,注意使用 long long 类型。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define MOD 1000000009
typedef long long LL;
const int maxn = 2;
struct Matrix{
    LL m[maxn][maxn];
}ans, res;
 
Matrix mul(Matrix a, Matrix b){
    Matrix tmp;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            tmp.m[i][j] = 0;
        }
    }
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
                tmp.m[i][j] += ((a.m[i][k] % MOD) * (b.m[k][j] % MOD)) % MOD;
                tmp.m[i][j] %= MOD;
            }
        }
    }
    return tmp;
}
 
void mpoww(LL n){
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            if(i == j){
                ans.m[i][j] = 1;
            }
            else ans.m[i][j] = 0;
        }
    }
    while(n){
        if(n & 1){
            ans = mul(ans, res);
        }
        res = mul(res, res);
        n >>= 1;
    }
}
 
int main()
{
    LL n;
    cin >> n;
    res.m[0][0] = 1;
    res.m[0][1] = 1;
    res.m[1][0] = 1;
    res.m[1][1] = 0;
    mpoww(n);
    cout << ans.m[0][1] << endl;
    return 0;
}

 

D: 一个简单的数学问题

构造出转移矩阵 $$ \begin{bmatrix} a0 & a1 & a2 & a3&a4&a5&a6&a7&a8&a9 \\ 1 & 0&0&0&0&0&0&0&0&0\\ 0&1&0&0&0&0&0 &0 &0 &0 \\ 0 &0 &1&0 &0 &0 &0 &0 &0 &0 \\ 0 &0 &0 &1&0 &0 &0 &0 &0 &0 \\ 0&0 &0 &0 &1&0 &0 &0 &0 &0 \\ 0&0 &0 &0 &0 &1&0 &0 &0 &0 \\ 0 &0 &0 &0 &0 &0 &1&0 &0 &0 \\ 0 &0 &0 &0&0 &0 &0 &1&0 &0 \\ 0 &0 &0 &0 &0 &0 &0 &0 &1&0 \end{bmatrix} \tag{4} $$,初始矩阵为 $$ \begin{bmatrix} f(9) \\ f(8) \\f(7) \\ f(6)\\f(5)\\f(4)\\f(3)\\f(2)\\f(1)\\f(0) \end{bmatrix} \tag{4} $$,所以

$$ \begin{bmatrix} a0 & a1 & a2 & a3&a4&a5&a6&a7&a8&a9 \\ 1 & 0&0&0&0&0&0&0&0&0\\ 0&1&0&0&0&0&0 &0 &0 &0 \\ 0 &0 &1&0 &0 &0 &0 &0 &0 &0 \\ 0 &0 &0 &1&0 &0 &0 &0 &0 &0 \\ 0&0 &0 &0 &1&0 &0 &0 &0 &0 \\ 0&0 &0 &0 &0 &1&0 &0 &0 &0 \\ 0 &0 &0 &0 &0 &0 &1&0 &0 &0 \\ 0 &0 &0 &0&0 &0 &0 &1&0 &0 \\ 0 &0 &0 &0 &0 &0 &0 &0 &1&0 \end{bmatrix} \tag{4} $$^{n-9} * $$ \begin{bmatrix} f(9) \\ f(8) \\f(7) \\ f(6)\\f(5)\\f(4)\\f(3)\\f(2)\\f(1)\\f(0) \end{bmatrix} \tag{4} $$ = $$ \begin{bmatrix} f(n) \\ f(n-1) \\f(n-2) \\ f(n-3)\\f(n-4)\\f(n-5)\\f(n-6)\\f(n-7)\\f(n-8)\\f(n-9) \end{bmatrix} \tag{4} $$

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 11;
struct Matrix{
    int m[maxn][maxn];
}f, e;  // f为转移矩阵,e为初始矩阵
int k, MOD;
 
Matrix mul(Matrix a, Matrix b){
    Matrix tmp;
    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            tmp.m[i][j] = 0;
        }
    }
    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            for(int k = 0; k < 10; k++){
                tmp.m[i][j] += ((a.m[i][k] % MOD) * (b.m[k][j] % MOD)) % MOD;
                tmp.m[i][j] %= MOD;
            }
        }
    }
    return tmp;
}
  
Matrix mpoww(Matrix res, int n){
    Matrix ans;
    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            if(i == j){
                ans.m[i][j] = 1;
            }
            else ans.m[i][j] = 0;
        }
    }
    while(n){
        if(n & 1){
            ans = mul(ans, res);
        }
        res = mul(res, res);
        n >>= 1;
    }
    return ans;
}
 
 
int main()
{
    while(scanf("%d %d", &k, &MOD) == 2){
        memset(f.m, 0, sizeof f.m);
        for(int i = 0; i < 10; i++){
            scanf("%d", &f.m[0][i]);
            if(i < 9) f.m[i+1][i] = 1;
            e.m[9 - i][0] = i;
        }
        if(k < 10){
            cout << k << endl;
        }
        else {
            Matrix tmp = mpoww(f, k - 9);
            int ans = 0;
            for(int i = 0; i < 10; i++){
                ans = (ans + (tmp.m[0][i] * e.m[i][0]) % MOD) % MOD;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

 

E: 小小粉刷匠(完整船星版本)

公式为 4 * 3^{n-2},直接快速幂即可。

#include <iostream>
#include <cmath>
using namespace std;
const int mod = 1000000009;
typedef long long ll;
ll poww(ll a, ll n)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)
            res = (res * a) % mod;
        a = (a * a) % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    ll n;
    while (cin >> n)
    {
        cout << (4 * poww(3, n - 2)) % mod << endl;
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值