51nod-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示例
2
3 3
1 1 3
3 6
2 3 6
Output示例
4
2

题解:我是真的无语。。。直接暴力背包居然就能过。。。用map存当前存在的可以被k整除的值的数量,每次进来一个值,如果能被k整除,则乘上之前所有的情况看看能不能被k整除,如果可以,加入map里!

AC代码

#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
typedef long long ll;
 
using namespace std;

const ll mod = 1e9 + 7;

int main(){
	ll t, n, k;
	ll m;
	scanf("%lld", &t);
	while(t--){
		map<ll, ll> s1;
		map<ll, ll> s2;
		map<ll, ll> :: iterator iter;
		scanf("%lld %lld", &n, &k);
		for(int i = 0; i < n; i++){
			scanf("%lld", &m);
			if(k % m != 0)
				continue;
			s2 = s1;
			s1[m] += 1;
			for(iter = s2.begin(); iter != s2.end(); iter++){
				ll x = iter -> first;
				if(k % (x * m) == 0){
					s1[x * m] += iter -> second;
					s1[x * m] %= mod;
				}
			}
		}
		printf("%lld\n", s1[k]);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_37064135/article/details/80294353