#include<stdio.h>
#include<math.h>
int x[20],n,k,i;

//判断是否质数

int isprime(int n){
    for(i=2;i<=sqrt(n);i++){
        if(n%i==0)return 0;
    }
    return 1;
}

*******************************************************************

//重点

//choose_left_num为剩余的k,already_sum为前面累加的和,start和end为全组合剩下数字的选取范围;调用递归生成全组合,在过程中逐渐把K个数相加,当选取的数个数为0时,直接返回前面的累加和是否为质数即可

//每次选数的时候,我要知道选数范围、累加之和以及剩余次数

int rule(int choose_left_num,int already_sum,int start,int end){    //自己做时就没有考虑递归传入选区范围
    if(choose_left_num==0)return isprime(already_sum);
    int sum=0;          //有几个素数
    for(i=start;i<=end;i++){           //没有加够k个数时,就会因i>end而结束循环
        sum+=rule(choose_left_num-1,already_sum+x[i],i+1,end);
    }
    return sum;
}

/*自己写的(错误)

int p(int n,int c){
 int s=0;
    if(c==1){
        return a[n];
    }
    for(;n<2;n++){
       s+=a[n]+p(n+1,c-1);
    }
    return s;
}

*/

*****************************************************************

int main(){
    scanf("%d %d",&n,&k);
    for(i =0;i<n;i++)scanf("%d",&x[i]);
    printf("%d",rule(k,0,0,n-1));//调用递归解决问题
}

(递归)P1036 选数的更多相关文章

  1. luogu P1036 选数 x

    P1036 选数 题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别 ...

  2. 【搜索】【入门】洛谷P1036 选数

    题目描述 已知 n个整数x1​,x2​,…,xn​,以及1个整数k(k<n).从nn个整数中任选kk个整数相加,可分别得到一系列的和. 例如当n=4,k=3,4个整数分别为3,7,12,19时, ...

  3. p1036 选数(不详细勿看,递归)

    题目:传送门 这题,不会做,而且看了好久才看懂题解的,然后在题解的基础上补了一个 if(start>end) return 0 感觉这样对于我更直观 转载自:大神博客的传送门,点击进入 先声明, ...

  4. 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举

    题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...

  5. P1036 选数 题解

    题目链接https://www.luogu.org/problemnew/show/P1036 题目描述 已知 nnn 个整数 x1,x2,-,xnx_1,x_2,-,x_nx1​,x2​,-,xn​ ...

  6. 洛谷 P1036 选数

    嗯.... 这种类型的题在新手村出现还是比较正常的, 但是不知道为什么它的分类竟然是过程函数与递归!!!(难道这不是一个深搜题吗??? 好吧这就是一道深搜题,所以千万别被误导... 先看一下题目: 题 ...

  7. P1036 选数(DFS)

    题目 https://www.luogu.org/problemnew/show/P1036 思路 搜索,使用递归实现dfs,所有数字遍历一遍,当取遍所有数组的index(扫了一遍,并非一定是选取了) ...

  8. 洛谷P1036选数(素数+组合数)

    题目链接:https://www.luogu.org/problemnew/show/P1036 主要考两个知识点:判断一个数是否为素数.从n个数中选出m个数的组合 判断一个数是否为素数: 素数一定是 ...

  9. 洛古P1036 选数 题解

    [我是传送门] 这是一道很经典的深搜与回溯(难度一般) 可是就这个"普及-" 让本蒟蒻做了一晚上+半个上午(实际我不会深搜回溯,全靠框架+去重); 下面让我分享下本蒟蒻的(全排列+ ...

随机推荐

  1. J - Worker

    Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard problem. The ...

  2. 干干净净的grep

    用grep -rn "xxxx" ./   搜索. 有时候出现大量的 错误信息 主要有 1.Is a directory 2.no such file or directory 前 ...

  3. redis在.Net程序中使用

    1.设置访问密码 config 2.连接redis服务器 private static string redisHost = ConfigHelper.GetAppSetting("redi ...

  4. 【CF1154G】Minimum Possible LCM

    题意 给你 \(n\) 个数 \(a_i\) ,求出 \(\text{lcm}\) 最小的一对数. \(n\le 10^6, a_i\le 10^7\) 题解 直接枚举 ,找到当前数最小的两个倍数,统 ...

  5. ubuntu18.04 基于Hadoop3.1.2集群的Hbase2.0.6集群搭建

    前置条件: 之前已经搭好了带有HDFS, MapReduce,Yarn 的 Hadoop 集群 链接: ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式 ...

  6. CentOS7上防火墙操作

    firewalld打开关闭防火墙与端口 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl statu ...

  7. Sklearn 预处理数据

    ## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...

  8. NSPredicate实现数据筛选

    一:基本语法 1.什么是NSPredicate apple官方文档这样写的: The NSPredicate class is used to define logical conditions us ...

  9. python转换ascii码

    字符转数字 ord("A") 数字转字符 chr(65)

  10. 【pwnable.kr】col

    pwnable从入门到放弃第二题, ssh col@pwnable.kr -p2222 (pw:guest) 同样是登录,然后看到了col.c.col.flag三个文件,读一下col.c #inclu ...