A Simple Math Problem

本文介绍了一种使用矩阵快速幂解决特定递推数列问题的方法,针对大规模数值计算,提供了高效的算法实现。通过构造特定矩阵,利用快速幂运算加速求解过程,适用于k小于2*10^9,m小于10^5的情况。

摘要生成于 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

题目大意:
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 .
利用公式求出f(k) % m并输出。注意 k<2*10^9 , m < 10^5.

解题思路:利用矩阵快速幂进行求解,重点在于怎样构造矩阵。
在这里插入图片描述
利用矩阵快速幂所求第一行即是答案。
node ans=a^(n-9)*b;

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int m;
ll k;

struct node {
	ll materix[50][50];
};
node a,b;

node mul(node a,node b){//矩阵乘法
	node ans;
	memset(ans.materix,0,sizeof(ans.materix));
	for(int i=1;i<=10;i++){
		for(int j=1;j<=10;j++){
			for(int k=1;k<=10;k++){
				ans.materix[i][j]=(ans.materix[i][j]+a.materix[i][k]*b.materix[k][j]%m)%m;
			}
		}
	}
	return ans;
}

node ksm(node a,ll b){//矩阵快速幂
	node ans;
	memset(ans.materix,0,sizeof(ans.materix));
	for(int i=1;i<=10;i++) ans.materix[i][i]=1;
	while(b){
		if(b&1) ans=mul(ans,a);
		a=mul(a,a);
		b>>=1;
	}
	return ans;
	
}

int main(){
	while(cin>>k>>m){
		memset(a.materix,0,sizeof(a.materix));
		for(int i=1;i<=10;i++){
			cin>>a.materix[i][1];
		}
		for(int i=1;i<=10;i++){
			a.materix[i][i+1]=1;
		}
		if(k<10){
			cout<<k%m<<endl;
			continue;
		}
		else {
			node b;
			for(int i=1;i<=10;i++){
				b.materix[1][i]=10-i;
			}
			node ans=ksm(a,k-9);
			ans=mul(b,ans);
			cout<<ans.materix[1][1]%m<<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、付费专栏及课程。

余额充值