SRM 508(2-1000pt)

DIV2 1000pt

题意:给定整数n和r,求有多少个这样的数列,a1,a2...an,使得a1 + a2 +...+an = a1|a2|a3|...|an,(按位或)。输出这样数列的个数mod 1000000009。

   n <= 10,r <= 15000。

解法:先按位分析这道题,若将a1,a2..an转化成二进制形式并对齐如下,则可将题目转化为求每一列最多含有一个1,每一行所对应的数小等于r的矩阵有多少个。

                       SRM 508(2-1000pt)

   这样的话,下意识地想到用状态压缩的DP来做,但是这样做的时间复杂度为O(10×15000×15000),不能接受。最后我也没想出更好的方法,只好看了题解。

   对于某一行,若要使得它对应的数小于r,只需要在某一列,r的二进制形式为1,它为0;在这一列之前, 该行所有值与r相同;在这之后,该行每一列可以为任意值。

   设数组d[i][j]表示从左向右扫描的情况下,从第i位扫到第0位,已经有(n-j)个数小于r的情况下,共有多少个符合题意的数列。具体状态转移方程可以看我得代码,详细的注解见官方题解的代码,http://apps.topcoder.com/wiki/display/tc/SRM+508

tag:dp, good

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "YetAnotherORProblem2.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64;
typedef pair<int, int> pii; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ;
const int mod = ; int r, n;
int64 d[][]; int64 rec (int t, int num)
{
if (t == -) return ; int64 &ret = d[t][num];
int tmp = r & ( << t); if (ret != -) return ret; if (num == n){
if (tmp)
return ret = (rec(t-, ) + n * rec(t-, )) % mod;
return ret = rec(t-, num);
}
if (num == ){
if (tmp)
return ret = (rec(t-, ) + rec(t-, ) + (n-) * rec(t-, )) % mod;
return ret = n * rec(t-, ) % mod;
}
return ret = (n+) * rec(t-, ) % mod;
} class YetAnotherORProblem2
{
public:
int countSequences(int N, int R){
r = R; n = N;
CLR1 (d);
return (int)((rec(, n)+mod) % mod);
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
//void test_case_0() { int Arg0 = 2; int Arg1 = 15000; int Arg2 = 4628299; verify_case(0, Arg2, countSequences(Arg0, Arg1)); }
void test_case_0() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, countSequences(Arg0, Arg1)); }
void test_case_1() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, countSequences(Arg0, Arg1)); }
void test_case_2() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, countSequences(Arg0, Arg1)); }
void test_case_3() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, countSequences(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
YetAnotherORProblem2 ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
上一篇:给你的WordPress站点添加下雪特效


下一篇:python中函数与函数之间的调用,总是晕菜,整理如下,有不对或者补充的请提出来~