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的矩阵有多少个。
这样的话,下意识地想到用状态压缩的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
SRM 508(2-1000pt)的更多相关文章
- SRM 508 DIV1 500pt(DP)
题目简述 给定一个大小为 n的序列(n<=10)R,要求你计算序列A0, A1, ..., AN-1的数量,要求A序列满足A0 + A1 + ... + AN-1 = A0 | A1 | ... ...
- SRM DIV1 500pt DP
SRM 501 DIV1 500pt SRM 502 DIV1 500pt SRM 508 DIV1 500pt SRM 509 DIV1 500pt SRM 511 DIV1 500pt SRM 5 ...
- TC250专场
SRM 623 DIV2 1000pt 题意:给出一个最多50*50的矩阵,每个单元可能为'.'.'P'.'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形 ...
- SRM149 - SRM150(少SRM150-DIV1-LV3)
SRM 149 DIV2 1000pt 题意: 对于n个人,第i人有pi的钱.将他们分成不超过四个组,每组统一交费x,对每个人,若他拥有的钱超过x则交费,否则不交费.问最多能使这些人交多少钱. 1&l ...
- Topcoder 好题推荐
SRM SRM147 DIV1 1000pt DP SRM148 DIV1 1100pt 递归 SRM149 DIV1 1000pt math SRM150 DIV1 500pt DP SRM469 ...
- SRM144 - SRM 148(少144-DIV1-LV3,147-DIV2-LV3)
SRM 144 DIV 1 500pt tag:组合 题意:彩票中奖.给定n, m,从1-n中选择m个数组成数列a1, a2, a3...am.对于数列{am}分别满足以下条件的概率: (1)数列所有 ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
- SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)
SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...
随机推荐
- jquery,javascript -设置某一ul下的li下的 a的属性
//javascriptvar ul = document.getElementById('ul); var as = ul.getElementsByTagName('a'); for(var i ...
- post和get的区别?
1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...
- 打造属于前端的Uri解析器
今天和大家一起讨论一下如何打造一个属于前端的url参数解析器.如果你是一个Web开发工程师,如果你了解过后端开发语言,譬如:PHP,Java等,那么你对下面的代码应该不会陌生: $kw = $_GET ...
- 十一、C# 泛型
为了促进代码重用,尤其是算法的重用,C#支持一个名为泛型的特性. 泛型与模块类相似. 泛型使算法和模式只需要实现一交.而不必为每个类型都实现一次.在实例化的时候,传入相应的数据类型便可. 注:可空值类 ...
- hdoj 2041 超级阶梯
代码: #include <stdio.h>int main(){int n;int i;int m;int count;int dp[50];while(scanf("%d&q ...
- Swift中的dispatch_once 单例模式
以下有三种方法实现单例模式,支持懒初始化和线程安全 全局变量 结构 dispatch_once 全局变量: 这里使用了全局变量而非类变量,是因为不支持类变量 private let _Singleto ...
- skip-grant-tables的作用
skip-grant-tables:非常有用的mysql启动参数(不启动grant-tables授权表) skip-grant-tables:非常有用的mysql启动参数 介绍一个非常有用的mys ...
- C#中foreach遍历学习笔记
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- const 笔记
.指向const的指针例如:double a=1.01;const double * b=&a;*b=2.1; //这显然是错误的a=2.1; //这是正确的,a和*b的值都会变成2.01,有 ...
- java Active Object模式(下)
Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...