Choose and divide UVA - 10375

本文详细探讨了UVA在线判题系统中的第10375题,重点讲解如何有效地进行'Choose and divide'策略分析,通过实例解析算法思路,帮助读者理解并解决此类问题。

Choose and divide

  UVA - 10375 
The binomial coefficient C(m, n) is defined as  C(m, n) = m! / (m − n)! n!
Given four natural numbers p, q, r, and s, compute the the result of dividing C(p, q) by C(r, s).
Input
Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values
for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000  with p ≥ q and r ≥ s.
Output
For each line of input, print a single line containing a real number with 5 digits of precision in the fraction,
giving the number as described above. You may assume the result is not greater than 100,000,000.
Sample Input
10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
Sample Output
0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960
题意:已知C(m,n) = m!/(n!(m-n)!),输入整数p,q,r,s,计算C(p,q)/C(r,s).输出保证不超过10^8,保留五位小数。
题解:一道需要用到唯一分解定理的题目,数据范围是10000,先将10000的质数求出,然后统计最后的唯一分解式中每一个素数的指数即可。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

const int maxn = 10005;

int prime[maxn];
int isprime[maxn];
int k;
int n,m,p,q;
int e[maxn];
void intc()
{
    k = 0;
    memset(e,0,sizeof(e));
    memset(prime,0,sizeof(prime));
    for(int i=2;i<=maxn;i++)
    {
        if(prime[i]==0)isprime[k++]=i;
        for(int j=i*2;j<=maxn;j+=i)prime[j]=1;
    }
}
void get_ans(int n,int d)
{
    for(int i=0;i<k;i++)
    {
        while(n%isprime[i]==0)
        {
            n/=isprime[i];
            e[i]+=d;
        }
    }
}
void add_integer(int n,int d)
{
    for(int i=1;i<=n;i++)get_ans(i,d);
}
int main()
{
    while(scanf("%d %d %d %d",&n,&m,&p,&q)!=EOF)
    {
        intc();
        add_integer(n,1);
        add_integer(m,-1);
        add_integer(n-m,-1);
        add_integer(p,-1);
        add_integer(q,1);
        add_integer(p-q,1);
        double ans = 1;
        for(int i=0;i<k;i++)
        {
            ans = ans *pow(isprime[i],e[i]);
        }
        printf("%.5f\n",ans);
    }
    return 0;
}

### K-Means聚类算法中的分治法实现与优化 #### 分治策略概述 分治法是一种通过将一个问题分解成若干个小规模子问题来求解的技术。对于K-Means聚类算法而言,可以采用分治策略减少计算量并提高效率。 #### 数据分割 数据集可以根据空间分布特性被划分为多个互不相交的小区域[^1]。这种划分可以通过预先设定边界框或其他几何形状完成。每个子区域内独立运行标准的K-Means过程: ```python import numpy as np from sklearn.cluster import MiniBatchKMeans def divide_data(X, num_partitions=4): """ 将输入的数据X按照指定数量分区 """ n_samples = X.shape[0] indices = np.random.permutation(n_samples) partition_size = int(np.ceil(n_samples / num_partitions)) partitions = [] for i in range(num_partitions): start_idx = i * partition_size end_idx = min((i + 1) * partition_size, n_samples) partitions.append(X[indices[start_idx:end_idx]]) return partitions partitions = divide_data(data_matrix, num_partitions=8) local_clusters = {} for idx, part in enumerate(partitions): mbkmeans = MiniBatchKMeans(n_clusters=k).fit(part) local_clusters[idx] = (mbkmeans.cluster_centers_, mbkmeans.labels_) ``` #### 局部中心点聚合 各个子区间的局部质心会被收集起来作为全局范围内的初始簇中心候选集合。接着利用这些新的种子再次执行完整的K-Means迭代直到收敛: ```python global_centroids = np.vstack([centers for centers,_ in local_clusters.values()]) final_kmeans = MiniBatchKMeans(n_clusters=k).fit(global_centroids) final_labels = final_kmeans.predict(data_matrix) ``` 这种方法不仅能够加速训练速度而且有助于克服大规模数据集中存在的内存瓶颈问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值