51 nod 1354 选数字(stl+dp)
2017-09-18 17:38
441 查看
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
Output
Input示例
Output示例
用背包求方法数,但是数据范围太大 所以将k的因子离散化成下标 用map记录方法数
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 20000+1105;
typedef long long LL;
char str[510][510];
const int mod = 1e9+7;
LL a
;
int vis
;
set<LL>st;
set<LL>::iterator it;
map<LL,LL>q, pos;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;LL k;
scanf("%d %lld", &n, &k);
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%lld", &a[i]);
if(k%a[i]==0) vis[i]=1;
}
st.clear();
LL h=(LL)(sqrt(1.0*k)+0.00001);
pos.clear();
for(LL i=1;i<=h;i++)
{
if(k%i==0)
{
st.insert(i);
st.insert(k/i);
}
}
q.clear();
int cnt=0;
for(it=st.begin();it!=st.end();it++)
{
h=*it;
pos[cnt]=*it;
cnt++;
}
q[1]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i]) continue;
for(int j=cnt-1;j>=0&&pos[j]>=a[i];j--)
{
if(pos[j]%a[i]==0)
{
q[pos[j]]=(q[pos[j]]+q[pos[j]/a[i]])%mod;
}
}
}
printf("%lld\n",q[k]);
}
return 0;
}
基准时间限制: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示例
2 3 3 1 1 3 3 6 2 3 6
Output示例
4 2
用背包求方法数,但是数据范围太大 所以将k的因子离散化成下标 用map记录方法数
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 20000+1105;
typedef long long LL;
char str[510][510];
const int mod = 1e9+7;
LL a
;
int vis
;
set<LL>st;
set<LL>::iterator it;
map<LL,LL>q, pos;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;LL k;
scanf("%d %lld", &n, &k);
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%lld", &a[i]);
if(k%a[i]==0) vis[i]=1;
}
st.clear();
LL h=(LL)(sqrt(1.0*k)+0.00001);
pos.clear();
for(LL i=1;i<=h;i++)
{
if(k%i==0)
{
st.insert(i);
st.insert(k/i);
}
}
q.clear();
int cnt=0;
for(it=st.begin();it!=st.end();it++)
{
h=*it;
pos[cnt]=*it;
cnt++;
}
q[1]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i]) continue;
for(int j=cnt-1;j>=0&&pos[j]>=a[i];j--)
{
if(pos[j]%a[i]==0)
{
q[pos[j]]=(q[pos[j]]+q[pos[j]/a[i]])%mod;
}
}
}
printf("%lld\n",q[k]);
}
return 0;
}
相关文章推荐
- HDU 1061 (51Nod 1004 )n^n的末位数字
- [DP] 51 Nod 1274——最长递增路径
- 51 nod 1705 七星剑(概率dp)
- 数数字(51nod 1770)
- 1354 选数字 DP背包 + 数学剪枝
- 简单DP(51nod 1092)
- 【贪心构造】51Nod 1385 凑数字
- 51 nod 1503 猪和回文(双线程DP)
- (51nod)1004 - n^n的末位数字
- 数字1的个数(51Nod 1009)
- 51 nod 1022 石子归并 V2(dp决策单调性)
- 51 Nod 1051 最大子矩阵和 (DP)
- 1183 编辑距离(51NOD)(dp)
- 51 NOD 1021 石子归并(二维dp,GarsiaWachs算法)
- [树形DP]51 Nod 1500——苹果曼和树
- [51Nod](1083) 矩阵取数问题 ---- 简单dp
- (51nod)1009 - 数字1的数量
- 51 NOD 1022 石子归并 V2(dp四边形加速)
- [51 nod]1009 数字1的数量
- 51 nod 1385 凑数字(贪心+构造)