xcoj1009 密码

题目描述

小翔同学的宿舍WIFI添加了密码,密码每天都会变更。而小翔每天都会给蹭网的同学们提供密码提示。现在请你根据密码提示,编写程序破译密码。
已知密码提示给出了n个整数 a1,a2,…,an,以及一个整数 t(t小于n)。从这n个整数中任选t个相加,可分别得到一系列的和。例如当 n=4,t=3,4 个整数分别为 3,7,12,19 时,可得全部组合的和分别为:
      3+7+12=22  3+7+19=29  7+12+19=38  3+12+19=34。
而和为素数的个数即为小翔宿舍WIFI的密码。
例如上例,只有一种的和为素数:3+7+19=29,于是密码即为1。

输入

n , t (1<=n<=20,t<n)
a1,a2,…,an (1<=ai<=5000000)

输出

一个整数(即为密码)。

样例输入

4 33 7 12 19

样例输出

1












我真是个傻x系列.........写了一下午


#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<deque>
#include<cmath>
#include<climits>
#include<list>
#include<utility>
#include<memory>
#include<cstddef>
#include<iterator>

using namespace std;
typedef long long LL;
const double pi = acos(-1.0);
///

int isprime(LL in) {
	for (LL i = 2; i <= in - 1; i++) {
		if (in%i == 0) return 0;
	}
	return 1;
}

int str[30];
int book[30];
int ans;

int n, t;

void sov(int step,int left,int sum) {
	
	
	if (left == 0) {
		if (isprime(sum) == 1) {
			ans++;
		}
		return;
	}
	if (step<=n-1) {
		//book[step] = 1;
		sov(step + 1, left - 1, sum + str[step + 1]);
		//book[step] = 0;
		sov(step + 1, left, sum);
	}
	

	
	
}

	
///
int main(int argc, char**argv) {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	

	while (cin >> n >> t) {

		for (int i = 1; i <= n; i++) {
			cin >> str[i];
		}
		memset(book, 0, sizeof(book));
		ans = 0;
		//book[1] = 1;
		
			sov(1, t - 1, str[1]);
			sov(1, t, 0);

		cout << ans << endl;
	}
	



	
	//system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值