A Simple Math Problem

本文介绍了一种使用矩阵快速幂解决特定递推数列问题的方法。对于给定的递推公式f(x),当x小于10时,f(x) = x;当x大于等于10时,f(x)为前10项的加权和。文章提供了完整的C++代码实现,并解释了如何构造关系矩阵以及如何应用快速幂算法高效地计算f(k) % m。

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

Lele now is thinking about a simple function f(x). 

If x < 10 f(x) = x. 
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 
And ai(0<=i<=9) can only be 0 or 1 . 

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m. 

Input

The problem contains mutiple test cases.Please process to the end of file. 
In each case, there will be two lines. 
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 ) 
In the second line , there are ten integers represent a0 ~ a9. 

Output

For each case, output f(k) % m in one line.

Sample Input

10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0

Sample Output

45
104

思路:这个是矩阵快速幂,没看出来的话不好做要是看出来就很好做了。

关系矩阵是  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

 

(矩阵快速幂一直不会写,只会直接粘贴代码 - -)

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int n;
long long k,m;
int N;
struct node{long long  a[11][11];};
node shu,ans,mp;
node matrix(node x,node y){
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            mp.a[i][j]=0;
            for(int p=1;p<=n;p++)
                mp.a[i][j]=(mp.a[i][j]+x.a[i][p] * y.a[p][j])%N;
        }
    return mp;
}
void work(long long k){
    while(k){
        if(k&1)
            ans=matrix(ans,shu);
        k>>=1;
        shu=matrix(shu,shu);
    }
}
int main(){
    while(~scanf("%lld%d",&k,&N))
    {
    n=10;
//    sum=0;
    memset(ans.a,0,sizeof(ans.a));
    memset(shu.a,0,sizeof(shu.a));
    for(int i=1;i<=n;i++){
        ans.a[i][i]=1;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>shu.a[1][i];
    }
    int t=1;
    for(int i=2;i<=n;i++)
    {
        shu.a[i][t]=1;
        t++;
    }
    work(k-9);
    long long sum=0;
    t=9;
    for(int i=1;i<=n;i++){
        sum+=ans.a[1][i]*t;
        sum=sum%N;
        t--;
    }
    cout<<sum<<endl;
    }
    return 0;
}

 

请帮我分析解释一下:Searching to scale. To consider both salient and non-salient weights, we choose to automatically search for an optimal (per input channel) scaling factor that minimizes the output difference after quantization for a certain layer. This scaling factor should minimize the difference in output that occurs when quantizing the weights for a given layer, helping to maintain accuracy after quantization. Since the quantization function is not differentiable, we are not able to directly optimize the problem with vanilla backpropagation. There are some techniques relying on approximated gradients, which we found still suffer from unstable convergence. To make the process more stable, we define a search space for the optimal scale by analyzing the factors that will affect the choice of scaling factor. As shown in the last section, the saliency of weight channels is actually determined by the activation scale. Therefore, we simply use a very simple search space: s = sα X, α* = argminα L(sα X ) sX is the average magnitude of activation (per-channel), and we use a single hyperparameter α to balance between the protection of salient and non-salient channels. We can find the best α by a fast grid search over the interval of [0, 1]. We further apply weight clipping to minimize the MSE error of quantization. One of the key advantages of AWQ is its simplicity and efficiency. Unlike methods that rely on back-propagation or complex reconstruction processes, AWQ does not require fine-tuning or extensive calibration data. This makes it particularly well-suited for quantizing large pre-trained models, including instruction-tuned LMs and multi-modal LMs.
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值