Codeforces 327E Axis Walking 状压dp(水

题目链接:点击打开链接

题意:

给定n个数,随意排列。

给定k个违禁数b[]。

问:有多少个排列使得这个排列的 n项前缀和中不出现违禁数。

(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党表示被tle,心疼自己T^T

#include 
const int N = 24;
int dp[1<=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<

任性附上java tle代码

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<0)
				{
					dp[i] += dp[i^(1<=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<> 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 >>= 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 : -x;
	}

	double abs(double x) {
		return x > 0 ? x : -x;
	}

	long abs(long x) {
		return x > 0 ? x : -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);
	}
}


你可能感兴趣的:(状压DP,codeforce,水题)