codeforces1043F Make It One

28 篇文章 0 订阅
13 篇文章 0 订阅

Janusz is a businessman. He owns a company “Januszex”, which produces games for teenagers. Last hit of Januszex was a cool one-person game “Make it one”. The player is given a sequence of n integers ai.
It is allowed to select any subset of them, and the score is equal to the greatest common divisor of selected elements. The goal is to take as little elements as it is possible, getting the score 1. Now Janusz wonders, for given sequence, how much elements should the player choose?

Input
The first line contains an only integer n (1≤n≤300000) — the number of integers in the sequence.
The second line contains n integers a1,a2,…,an (1≤ai≤300000).

Output
If there is no subset of the given sequence with gcd equal to 1, output -1.
Otherwise, output exactly one integer — the size of the smallest subset with gcd equal to 1.


根据题目所给的数据范围可知,7个最小连续质数将超出所给的数据范围,所以子集的中元素的个数不会超过7个。
先考虑如下情况,对于所有由质数 n 倍数组成的长度为一定值的子集,那么这些子集可能的最大GCD可能为n ,也有可能为 n的某个倍数 。如果可以得出所有最大GCD为 n倍数 的子集个数,就可以求得最大GCD为 n 的子集个数。
定义dp[i][j]:为选择 i 个元素组成一个子集,其最大GCD为 j 的情况数。即所有可能的情况,减去最大GCD不是 j 的子集数。
递推关系式cnt为数组中可以被 j 整除的元素的个数,组合数的计算可以预处理阶乘和其逆元,从而可以以O(1)的复杂度计算出


#include <stdio.h>
#include <climits>
#include <algorithm>
#include <vector>
#include <utility>
#define ll long long
#define rep(index,star,finish) for(int index=star;index<finish;index++)
#define drep(index,finish,star) for(int index=finish;index>=star;index--)
#define Push(num) push_back(num)
using namespace std;
const int maxn=3e5+7;
const int mod=1e9+7;

int len;            //input size
int store[maxn];    //data of input
int con[maxn];      //the number of i's multiple
int molecule[maxn];     //molecule[i]: i!
int invMol[maxn];       //invMol[i]: 1/i!
int dp[20][maxn];       //dp[i][j] inf:  i:size of subset j:gcd of subset
ll quickpow(ll a, ll b);
int combine(int a,int b);
int main(){
    //get factorial
    molecule[0]=1;
    rep(i,1,maxn)
        molecule[i]=(1LL*i*molecule[i-1])%mod;
    //get inverse element(factorial)
    invMol[maxn-1]=quickpow(molecule[maxn-1],mod-2);
    drep(i,maxn-1,1)
        invMol[i-1]=(1LL*invMol[i]*(i))%mod;
    int peak=INT_MIN;
    scanf("%d",&len);
    rep(i,0,len){
        scanf("%d",&store[i]);
        peak=max(peak,store[i]);
        con[store[i]]++;
        if(store[i]==1){
            printf("1\n");
            return 0;
        }
    }
    //the number of i's multiple
    con[1]=len;
    rep(i,2,peak)
        for(int j=i+i;j<=peak;j+=i)
            con[i]+=con[j];
    rep(i,2,18){
        drep(j,peak,1){

           if(con[j]==0)
                continue;

           dp[i][j]=combine(i,con[j]);
           for(int k=j+j;k<=peak;k+=j){
                dp[i][j]-=dp[i][k];
                if(dp[i][j]<0)
                    dp[i][j]+=mod;
           }
        }
        if(dp[i][1]>0){
            printf("%d\n",i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}
int combine(int a,int b){
    if(a>b)
        return 0;
    return (((1LL*molecule[b]*invMol[a])%mod)*1LL*invMol[b-a])%mod;
}
ll quickpow(ll a, ll b) {
    if (b < 0) return 0;
    int ret = 1;
    a %= mod;
    while(b) {
        if (b & 1) ret = (1LL*ret * a) % mod;
        b >>= 1;
        a = (a * a) % mod;
    }
    return ret%mod;
}


Ps:

  1. 0!=1
  2. 计算时注意数据的大小,会超出int范围。
  3. 乘法运算时, *1LL 防止溢出
  4. 由于数值较大,所以dp数组内存储的值必定经过一个较大的质数取余,在本段程序中选择的为 1e9+7。所以在减去其他情况的过程中可能致使情况数为负
    eg:设整数 a =1e9+8 ,dp数组可能存在某个位置存储的值为 a%1e9+7=1,若此时其他情况的总数为 849363,这样会导致该位置上的情况数为负
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值