Topic Link: Click to open the link
Test instructions
Given n number, randomly arranged.
Given k number of forbidden b[].
Q: How many permutations of this arrangement make the N-entries prefix and do not appear in the forbidden number.
(Formally,if it ' s a legal permutation, sum[i]! = B[j] (1<=i<=n, 1<=j<=k))
Sum[0] = 0; Sum[i] = sum[i-1]+a[permutaion[i]];
==java party said to be tle, distressed himself t^t
#include <stdio.h>const int N = 24;int Dp[1<<n], a[n], b[n];int N, k;const int mod = 1e9+7;int Main () {scanf ("% D ", &n); for (int i = 0; i < n; i++) scanf ("%d", &a[i]); scanf ("%d", &k); for (int i = 0; i < K; i++) scanf ("%d", &b[i]); Dp[0] = 1;int all = 1<<n; for (int i = 1, sum; i < all; i++) { sum = 0; for (int j = 0; J < N; j + +) if ((i& (1<<j)) >0) { Dp[i] + = dp[i^ (1<<j)]; if (dp[i]>=mod) dp[i]-= mod; Sum + = A[j]; } for (int j = 0; J < K; J + +) if (sum = = B[j]) dp[i] = 0;} printf ("%d\n", dp[(1<<n)-1]); return 0;}
Willful attached Java tle Code
Import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.math.biginteger;import Java.text.decimalformat;import Java.util.arraydeque;import Java.util.ArrayList;import Java.util.arrays;import Java.util.collection;import Java.util.collections;import Java.util.Comparator;import Java.util.deque;import Java.util.hashmap;import Java.util.iterator;import Java.util.linkedlist;import Java.util.map;import Java.util.priorityqueue;import Java.util.scanner;import Java.util.stack;import Java.util.stringtokenizer;import Java.util.treemap;import Java.util.treeset;import Java.util.Queue;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.FileOutputStream; public class Main {int[] dp = new Int[1<<n], a = new Int[n], B = new Int[n];int N, k;void work () throws Exception {N = Int (); for (int i = 0; i < n; i++) a[i] = Int (), k = Int (), for (int i = 0, i < K; i++) b[i] = Int ();DP [0] = 1;int all = 1<<n;for(int i = 1, sum; i < all; i++) {sum = 0;for (int j = 0; J < N; j + +) if ((i& (1<<j)) >0) {Dp[i] + = dp[i^ (1<<j)];if (dp[i]>=mod) dp[i]-= Mod;sum + = A[j];} for (int j = 0; J < K; J + +) if (sum = = B[j]) dp[i] = 0;} Out.println (dp[(1<<n)-1]);} public static void Main (string[] args) throws Exception {main wo = new main (), in = new BufferedReader (New Inputstreamreade R (system.in)); out = new PrintWriter (system.out);//in = new BufferedReader (New InputStreamReader ( new//file ("Input.txt")));//out = new PrintWriter (New File ("output.txt")); Wo.work (); Out.close ();} static int N = 24;static int M = N * 2;decimalformat df = new DecimalFormat ("0.0000"); static int inf = (int) 1e9;static lo ng inf64 = (long) 1e18;static double EPS = 1e-8;static double Pi = math.pi;static int mod = (int) 1e9 + 7;private String N Ext () throws Exception {while (str = = NULL | |!str.hasmoreelements ()) str = new StringTokenizer (In.readline ()); return STR.N Exttoken ();} private int int () throws Exception {return integer.parseint (Next ());} Private long Long () throws Exception {return Long.parselong (Next ());} Private double double () throws Exception {return double.parsedouble (Next ());} StringTokenizer str;static Scanner cin = new Scanner (system.in); static BufferedReader in;static printwriter out;/* * class edge{int from, to, DIS, NEX; Edge () {} edge (int from, int. to, int. * DIS, int nex) {this.from = from; this.to = to; this.dis = dis; this.nex = * NEX;}} Edge[] Edge = new edge[m<<1]; Int[] head = new Int[n]; int * EDGENUM; void Init_edge () {for (int i = 0; i < N; i++) head[i] = 1; * edgenum = 0;} void Add (int u, int v, int dis) {edge[edgenum ] = new * Edge (U, V, dis, head[u]); Head[u] = edgenum++; }/* */int upper_bound (int[] A, int l, int r, int val) {//Upper_bound (a+l,a+r,val)-a;int pos = R;r--;while (l <= r) {in T mid = (L + R) >> 1;if (A[mid] <= val) {L = mid + 1;} else {pos = Mid;r = Mid-1;}} return POS;} int Lower_bound (int[] A, int l, int r, int val) {//Upper_bound (a+l,a+r,val)-a;int pos = R;r--;while (l <= r) {int mid = (L + R) >> 1;if (A[mid] < val) {L = mid + 1;} else {pos = Mid;r = Mid-1;}} return POS;} int Pow (int x, int y) {int ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} Double Pow (double x, int y) {double ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} int pow_mod (int x, int y, int Mod) {int ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;ans%= mod;y >>= 1 ; x = x * x;x%= mod;} return ans;} Long Pow (long x, long y) {long ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} Long Pow_mod (long x, long y, long Mod) {Long ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;ans%= mod;y >&G t;= 1;x = x * x;x%= mod;} return ans;} int gcd (int x, int y) {if (x > y) {int tmp = X;X = Y;y = tmp;} while (x > 0) {y%= x;int tmp = x;x = Y;y = tmp;} return y;} int Max (int x, int y) {return x > y x:y;} int min (int x, int y) {return x < y x:y;} Double Max (double x, double y) {return x > y x:y;} Double min (double x, double y) {return x < y x:y;} Long Max (long x, long y) {return x > y x:y;} Long min (long x, long y) {return x < y x:y;} int abs (int x) {return x > 0 ×:-X;} Double abs (double x) {return x > 0 ×:-X;} Long ABS (long x) {return x > 0 ×:-X;} Boolean zero (double x) {return abs (x) < EPS;} Double sin (double x) {return Math.sin (x);} Double cos (double x) {return math.cos (x);} Double tan (double x) {return Math.tan (x);} Double sqrt (double x) {return math.sqrt (x);}}
Codeforces 327E Axis Walking pressure dp (water