PAT——1096 Consecutive Factors 甲级(超详细注释)

该博客主要介绍了PAT甲级竞赛中的一道题目1096ConsecutiveFactors,内容涉及寻找一个整数n的最长连续因子序列。博主通过AC代码展示了如何找出长度最长且起始值最小的连续因子序列,特别地,当n为1或质数时,给出了特殊处理。文章适合对算法和编程感兴趣的读者阅读。

1096 Consecutive Factors

题目

1096 Consecutive Factors

题意

输出长度最长的起始值最小的连续因子序列

AC代码

#include<bits/stdc++.h>
using namespace std;
long long int n,fac;//防止超限
int main()
{
   
   
	cin>>n;
	int first=0,len
### 基于GMRF和FCM的图像去噪方法 #### 高斯马尔科夫随机场(GMRF) 高斯马尔科夫随机场是一种强大的工具,在处理空间数据方面表现出色。对于二维图像而言,像素之间的邻域关系可以被建模成图上的节点连接形式。通过定义合适的势函数来描述相邻像素间的相互作用力,能够构建起满足特定条件的概率分布模型[^1]。 在实际操作过程中,通常会假设噪声服从零均值独立同分布特性,并且原始无损图片遵循给定参数下的平稳随机过程。因此,当观测到含杂讯版本之后,则可通过最大似然估计或者贝叶斯推断等方式求解最有可能对应的纯净状态向量\[X\]。 ```python import numpy as np from scipy import stats def gmrf_prior(x, beta=0.9): """计算GMRF先验概率""" n = len(x) diff_x = (x - np.roll(x, 1))[:-1] log_prob = -(beta / 2.) * np.sum(diff_x ** 2) return log_prob def estimate_clean_image(noisy_img, iterations=1000): """基于GMRF的最大后验估计恢复原图""" height, width = noisy_img.shape[:2] # 初始化猜测 clean_estimation = noisy_img.copy().flatten() for _ in range(iterations): current_log_likelihood = (-np.log(stats.norm.pdf(clean_estimation)) ).sum() + gmrf_prior(clean_estimation) proposed_change = np.random.randn(*clean_estimation.shape)*0.1 new_estimate = clean_estimation + proposed_change proposal_log_likelihood = (-np.log(stats.norm.pdf(new_estimate))).sum( ) + gmrf_prior(new_estimate) acceptance_ratio = min(1., np.exp(proposal_log_likelihood - current_log_likelihood)) if np.random.rand() < acceptance_ratio: clean_estimation = new_estimate return clean_estimation.reshape((height, width)) ``` 这段代码实现了简单的GMRF框架下对受损图像重建的过程模拟。这里采用Metropolis-Hastings采样机制更新每一个位置处灰度级数值直到收敛为止。 #### 模糊C均值聚类(FCM) 作为一种软划分技术,FCM允许样本属于多个簇集的同时赋予不同隶属程度权重。这使得其特别适合用来识别那些边界较为模糊的对象类别。具体来说就是寻找一组中心点以及相应的成员资格矩阵U,从而最小化目标函数Jm(U,V)[^2]: $$ J_m(U,V)=\sum_{i=1}^{c}\sum_{j=1}^{n}(u_{ij})^m d^2(\textbf{x}_j,\textbf{v}_i), $$ 其中$c$表示簇的数量,$n$代表总的数据条目数,m控制着模糊性的强度而$d(\cdot)$则是衡量两点间距离的标准测度。 为了结合上述两种理论优势共同完成降噪任务,可以在预处理阶段运用GMRF平滑初步滤除高频干扰成分;接着再借助FCM算法依据局部区域相似性原则重新分配各象素所属组别标签,最终达到既保持边缘轮廓清晰又削弱斑点效应的目的。 ```matlab function Y = fcm_denoise(X,k,beta,iters) % X: 输入待处理影像数组 % k: 设定期望分割出来的子区数目 % beta: 控制GMRF影响力度系数 % iters: MCMC迭代次数上限 [m,n]=size(X); Y=X; % 复制输入作为初始输出副本 for iter=1:iters % Step1: Apply GMRF smoothing on the estimated image. Y_smoothed = apply_gmrf(Y,beta); % Step2: Perform Fuzzy C-Means clustering based segmentation. centers = initialize_centers(k,Y_smoothed(:)); U = compute_membership_matrix(Y_smoothed(:)',centers); while not_converged(U,U_prev) U_prev = U; V = update_centroids(U,Y_smoothed(:)'); U = compute_membership_matrix(Y_smoothed(:)',V); end % Reconstruct denoised image using updated cluster assignments. [~,idx] = max(U,[],1); Y = reshape(V(idx,:), m, n); end function smoothedImg = apply_gmrf(img,alpha) % Implementation of applying Gaussian Markov Random Field... end function cts = initialize_centers(K,dataPts) % Initialization strategy for centroids selection ... end function membMat = compute_membership_matrix(dataPoints,Centrs) % Calculation method to get membership matrix according to formula... end function converged = not_converged(current,next) % Convergence criterion check between two consecutive states... end function newCentrs = update_centroids(MembMatrix,Datapoints) % Update rule for moving centroid positions towards optimal solution... end ``` 此MATLAB伪代码片段展示了如何交替执行GMRF平滑与FCM分群步骤以实现有效的图像净化流程。值得注意的是,具体的内部功能模块需由开发者自行补充完善。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值