1 条题解
-
0
C++ :
#include <cstdio> #include<cstring> #include <string> #include <iostream> #include <algorithm> #include <cmath> using namespace std; long long int a[30],b[30],n,m,we=0,v=0,g[30],w7,g1=1; bool sushu(int x) { int i,t=1; if(x==1){return false;} else if(x==2||x==3){return true;} for(i=2;i*i<=x;i++) { if(x%i==0) {t=0; break; } } if(t){return true;} else{return false;} } void dfs(int x,int y) {if(y==0) { if(sushu(v)) {we++;} return ; } else { for(int w1=0;w1<x;w1++) { if(b[w1]==0) { b[w1]=1; v+=a[w1]; dfs(x,y-1); b[w1]=0; v-=a[w1]; } } } } int main() { for(w7=1;w7<21;w7++) {g1*=w7; g[w7]=g1; } scanf("%lld %lld",&n,&m); for(int w0=0;w0<n;w0++){scanf("%d",&a[w0]);} dfs(n,m); printf("%lld",we/g[m]); return 0; }Java :
import java.util.Arrays; import java.util.Scanner; public class Main { static int N,k,ans,sum; static int[] a; static boolean[] book; static boolean check(int m){//素数判断 if(m==1||m==0) return false; int n=(int)Math.sqrt(m); for(int i=2;i<=n;i++){ if(m%i==0) return false; } return true; } static void dfs(int index,int bag){ if(index==k){//k个数,判断素数 if(check(sum)==true) ans++; return; }else{ for(int i=bag;i<N;i++){//从上一个被选中的数字之后选择数字组合,避免排列重复 if(book[i]==false){ book[i]=true; sum+=a[i]; dfs(index+1,i+1); sum-=a[i];//这里要注意,把尝试的数字减去 book[i]=false; } } } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); N=sc.nextInt(); k=sc.nextInt(); a=new int[N+1]; book=new boolean[N+1]; Arrays.fill(book, false); for(int i=0;i<N;i++) a[i]=sc.nextInt(); ans=0; sum=0; dfs(0,0); System.out.println(ans); } }
- 1
信息
- ID
- 1095
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者