codeforces 396A A. On Number of Decompositions into Multipliers(组合数学+数论)

本文介绍 CodeForces 396A 的解题思路,通过分解质因数并使用组合数学的方法,计算序列的积相同时不同序列的数量。给出 AC 代码实现。

题目链接:

codeforces 396A


题目大意:

给出n个数的序列,求取和这个序列的积相同但是序列本身不同的个数。


题目分析:

  • 组合数学的问题,对于每一个数我们可以将它分解质因数,然后统计整个序列的各个质因数的个数。
  • 那么符合要求的序列一定用这些质因数(每个质因数的个数保持不变)组成的,所以我们可以利用组合数学中的插板法,对每个质因数进行划分,划分给n个数(存在一些数没有分到的情况),那么就是 Cn1+n1
  • 根据乘法原则,总的方案数就是每个质因数的划分数之积。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#define MAX 30007

using namespace std;

typedef long long LL;
typedef map<int,LL> MIL;
const LL mod = 1e9+7;
LL fac[MAX],recFac[MAX];
int n,a[MAX],mark[100007];
MIL mp;

LL inv ( LL num , LL x )
{
    LL ret = 1;
    while ( x )
    {
        if ( x&1 )
        {
            ret *= num;
            ret %= mod;
        }
        num *= num;
        num %= mod;
        x >>= 1;
    }
    return ret;
}

void init ( )
{
    fac[0] = 1;
    for ( LL i = 1 ; i < MAX ; i++ )
    {
        fac[i] = fac[i-1]*i;
        fac[i] %= mod;
        recFac[i] = inv ( fac[i] , mod-2 );
    }
    memset ( mark , 0 , sizeof ( mark ) );
    mark[1] = mark[0] = 1;
    for ( int i = 2 ; i*i < MAX; i++ )
    {
        if ( mark[i] ) continue;
        for ( int j =i*i ; j < MAX ; j += i )
            mark[j] = 1;
    }
}

void handle ( int num )
{
    for ( int i = 2 ; i*i <= num ; i++ )
    {
        if ( num%i ) continue;
        if ( mark[i] ) continue;
        while ( num%i == 0 )
        {
            num /= i;
            mp[i]++;
        }
    }
    if ( num > 1 ) mp[num]++;
}

LL C ( int n , int m )
{
    if ( m == 0 ) return 1;
    return fac[n]*recFac[n-m]%mod*recFac[m]%mod;
}

int main ( )
{
    init ( );
    while ( ~scanf ( "%d" , &n ) )
    {
        mp.clear();
        for ( int i = 0 ; i < n ; i++ )
        {
            scanf ( "%d" , &a[i] );
            handle ( a[i] );
        }
        LL ans = 1;
        MIL::iterator it = mp.begin();
        for ( ; it != mp.end() ; it++ )
        {
            int x = it->second;
            ans *= C ( x+n-1 , n-1 );
            ans %= mod;
        }
        printf ( "%lld\n" , ans );
    }
}
### Codeforces Round 1005 Div. 2 A-F Problem Solutions #### A. Money Change 为了处理货币转换问题,可以将所有的金额都转化为分的形式来简化计算。通过遍历输入数据并累加各个部分的金额,最后求得剩余的钱数并对100取模得到最终结果[^2]。 ```cpp #include <iostream> using namespace std; int main() { int s, xi, yi; cin >> s; int total_cents = 0; for (int i = 0; i < s; ++i) { cin >> xi >> yi; total_cents += xi * 100 + yi; } cout << (s * 100 - total_cents) % 100 << endl; } ``` #### B. Odd and Even Pairs 此题目要求找到至少一对满足条件的索引:要么是一个偶数值的位置,或者是两个奇数值位置。程序会读入测试次数`t`以及每次测试中的数组长度`n`及其元素,并尝试找出符合条件的一对索引输出;如果没有这样的组合则返回-1[^3]。 ```cpp #include <cstdio> int main() { int t, n, num; scanf("%d", &t); while (t--) { int evenIndex = 0, oddIndex1 = 0, oddIndex2 = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &num); if (num % 2 == 0 && !evenIndex) evenIndex = i; else if (num % 2 != 0) { if (!oddIndex1) oddIndex1 = i; else if (!oddIndex2) oddIndex2 = i; } if ((evenIndex || (oddIndex1 && oddIndex2))) break; } if (evenIndex) printf("1\n%d\n", evenIndex); else if (oddIndex1 && oddIndex2) printf("2\n%d %d\n", oddIndex1, oddIndex2); else printf("-1\n"); } return 0; } ``` 由于仅提供了前两道题的具体描述和解决方案,在这里无法继续给出完整的C至F题解答。通常情况下,每一道竞赛编程题都有其独特的挑战性和解决方法,建议查阅官方题解或社区讨论获取更多帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值