1354 选数字 (背包)

来源:互联网 发布:删除表的sql语句 编辑:程序博客网 时间:2024/05/16 11:37


1354 选数字
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
当给定一个序列a[0],a[1],a[2],...,a[n-1] 和一个整数K时,我们想找出,有多少子序列满足这么一个条件:把当前子序列里面的所有元素乘起来恰好等于K。
样例解释:

对于第一个数据,我们可以选择[3]或者[1(第一个1), 3]或者[1(第二个1), 3]或者[1,1,3]。所以答案是4。


Input
多组测试数据。在输入文件的第一行有一个整数T(0< T <= 20),表示有T组数据。接下来的2*T行,会给出每一组数据每一组数据占两行,第一行包含两个整数n, K(1<=n<=1000,2<=K<=100000000)他们的含意已经在上面提到。第二行包含a[0],a[1],a[2],...,a[n-1] (1<= a[i]<=K) 以一个空格分开。所有输入均为整数。
Output
对于每一个数据,将答案对1000000007取余之后输出即可。
Input示例
23 31 1 33 62 3 6
Output示例
42
解题报告(by System Message) 
类似于背包的DP,以乘积为状态。先把等选数字里面不是K约数的去掉。然后找出K的约数,进行离散化。然后dp[i][j]表示前i个数字乘积为j的状态。Dp[i+1][j*a[i+1]]]+=dp[i][j].
Dp[i+1][j]+=dp[i][j];
总的复杂度是O(n*d(k)*log(d(k)))
D(k)表示k的因子数目。多一个log是因为离散化了,对应下标的时候要二分查找。

如果按照一般的背包肯定会t的。。1e3个数字,每个数字1e8个背包大小。。所以优化一下,去掉没用的状态,只留下是k的因子的状态就好了。。用map做很好,因为map里存的都是因子,根据这个转移,网上还有一种代码是先离散化。。。


#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <map>using namespace std;const int maxn = 1e3 + 5;const int Mod = 1000000007;int a[maxn];int main(){    int t, n, k;    cin >> t;    while(t--)    {        map<int, int> ans, temp;        scanf("%d%d", &n, &k);        for(int i = 1; i <= n; i++)        {            scanf("%d", &a[i]);            temp = ans;            for(map<int, int>::iterator it = temp.begin(); it != temp.end(); it++)            {                int tnum = it->first * a[i];                if(k%tnum) continue;                ans[tnum] = (ans[tnum] + it->second)%Mod;            }            ans[a[i]] = (ans[a[i]] + 1)%Mod;        }        printf("%d\n", ans[k]);    }    return 0;}

先离散化,但思路都是一样的

#include <iostream>#include <algorithm>#include <map>#include <cstdio>#include <cstring>using namespace std;const int MOD = 1e9 + 7;const int MAXN = 1010;const int MAXM = 100001;int a[MAXN];int b[MAXN];int dp[MAXM];map<int, int> temp;int main(void){    int T;    scanf("%d", &T);    int n, K;    while (T--)    {        int i;        temp.clear();        scanf("%d%d", &n, &K);        memset(dp, 0, sizeof(dp));        for (i = 0; i < n; i++)        {            scanf("%d", &a[i]);        }        //  求K约数        int cnt = 0;        for (i = 1; i * i < K; i++)        {            if (K % i)            {                continue;            }            b[cnt++] = i;            b[cnt++] = K / i;        }        if (i * i == K)        {            b[cnt++] = i;        }        sort(b, b + cnt);        for (i = 0; i < cnt; i++)        {            temp[b[i]] = i;        }        for (i = 0; i < n; i++)        {            if (K % a[i])            {                continue;            }            int tmp = temp[a[i]];            for (int j = temp[K]; j >= 0; j--)            {                if (dp[j] && K % (b[j] * a[i]) == 0)                {                    int tmp_ = temp[b[j] * a[i]];                    dp[tmp_] = (dp[j] + dp[tmp_]) % MOD;                }            }            dp[tmp] = (dp[tmp] + 1) % MOD;        }        printf("%d\n", dp[cnt - 1]);    }    return 0;}




0 0
原创粉丝点击