DIV1 250pt
题意:有很多袋子,里面装有苹果和橘子(也可能没有),给出每个袋子里有多少个苹果,多少个橘子。如果每个袋子里含有水果的总数都不小于x个,则可以从每个袋子里都拿出x个水果(拿出苹果和橘子的总数为x),将所有拿出的水果混合成一份礼物,问可能混合出的礼物的种数。
最多50个袋子,每个袋子里的苹果和橘子的数量 <= 10^6。
解法:枚举即可,枚举从每个袋子里拿出的水果的数量x,然后求出,拿出总数是x的情况下,苹果最多能拿多少个,最少能拿多少个,相减即可。
至于怎么求最多和最少,由于最多50个袋子,所以暴力即可。比赛的时候我很傻逼地用了更快的递推来求,然后赋值初始化写错了,喜大普奔。。。。。。。。。。
tag:brute-force
// BEGIN CUT HERE
/* */
// END CUT HERE
#line 7 "WinterAndPresents.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#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; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; inline int MyMod( int a , int b ) { return (a%b+b)%b;} int num[];
int num2[]; int64 max(int x, int64 y)
{
return x > y ? x : y;
} class WinterAndPresents
{
public:
long long getNumber(vector <int> an, vector <int> on){
clr0 (num); clr0 (num2);
int n = sz(an);
for (int i = ; i < n; ++ i){
++ num[an[i]];
++ num2[on[i]];
}
int pos = ;
for (int i = ; i < n; ++ i)
if (an[pos] + on[pos] > an[i] + on[i]) pos = i;
int64 up = an[pos] + on[pos], cnt = ;
int64 ret = , sum = n - num[], maxx = ;
int64 ts = n - num2[], minn = ;
while (cnt <= up){
maxx += sum;
minn += ts;
ret += maxx - max(, cnt*n-minn) + ;
sum -= num[cnt];
ts -= num2[cnt];
//out (cnt); out (ret); out (sum); out (ts);
++ cnt;
}
return ret;
} // 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(); if ((Case == -) || (Case == )) test_case_4(); }
//void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();}
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 long long &Expected, const long long &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 Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); long long Arg2 = 3LL; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {, , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {, , , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); long long Arg2 = 0LL; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {, , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); long long Arg2 = 16LL; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {, , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); long long Arg2 = 46LL; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); long long Arg2 = 1000002000000LL; verify_case(, Arg2, getNumber(Arg0, Arg1)); } // END CUT HERE };
//by plum rain
// BEGIN CUT HERE
int main()
{
//freopen( "a.out" , "w" , stdout );
WinterAndPresents ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
DIV1 500pt
题意:给出整数n和m,{1,2,3....n}的一个子集a,{1,2,3...m}的一个子集b,要求a和b满足两个条件:1、a和b的元素无交集;2、a中所有元素异或得到的值小于b中所有元素异或得到的值。求这样的集合对(a, b)有多少对。
解法:首先,设a异或的值为x,b异或的值为y,由于x小于y,所以在二进制形式中,一定存在某一位r,在r位之前x和y相同,第r位x为0,y为1。枚举r。
设d[i][j][k]表示前i个数,放进a或b中的所有元素的异或值为j,x第r位为k(0或1)的状态下,一共有多少个这样的集合对。然后只要取d[max(n,m)][t][0]即可,其中t满足的条件是(t >> r) == 1。注意到,本题的两个关键点,一个是用异或值为1表示,前r位x和y相同,第r位不同;另一个是,只要把一个数放进集合a或b都把它和j与一下,然后,因为在比r位高的位a和b两个集合的异或值相同,所以j的这些位异或值为0。
虽然这是一个dp,但是关键的那点还是很难想到的。。。。
tag:dp, think, good
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "WinterAndSnowmen.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 clr0(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 all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(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 pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ;
const int mod = ; int n, m;
int d[][][]; int gao(int r)
{
clr0 (d);
d[][][] = ;
int cur = , tmp = << ;
for (int i = ; i <= min(n, m); ++ i){
cur ^= ;
int flag = ((i & (<<r)) > );
for (int j = ; j < tmp; ++ j)
for (int k = ; k < ; ++ k){
d[cur][j][k] = (d[cur^][j][k] + d[cur^][j^i][k]) % mod;
d[cur][j][k] = (d[cur][j][k] + d[cur^][j^i][k^flag]) % mod;
}
}
if (n > m)
for (int i = m+; i <= n; ++ i){
cur ^= ;
int flag = ((i & (<<r)) > );
for (int j = ; j < tmp; ++ j)
for (int k = ; k < ; ++ k)
d[cur][j][k] = (d[cur^][j][k] + d[cur^][j^i][k^flag]) % mod;
}
if (n < m)
for (int i = n+; i <= m; ++ i){
cur ^= ;
for (int j = ; j < tmp; ++ j)
for (int k = ; k < ; ++ k)
d[cur][j][k] = (d[cur^][j][k] + d[cur^][j^i][k]) % mod;
}
int ret = ;
for (int j = ; j < tmp; ++ j)
if ((j >> r) == ) ret = (ret + d[cur][j][]) % mod;
return ret;
} class WinterAndSnowmen
{
public:
int getNumber(int N, int M){
n = N; m = M;
int ans = ;
for (int i = ; i < ; ++ i)
ans = (ans + gao(i)) % mod;
return ans;
} // 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(); if ((Case == -) || (Case == )) test_case_4(); }
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 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_1() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_2() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_3() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getNumber(Arg0, Arg1)); }
void test_case_4() { int Arg0 = ; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getNumber(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
WinterAndSnowmen ___test;
___test.run_test(-);
return ;
}
// END CUT HERE