DIV1 250pt
题意:电视目前停留在第100台,有一个遥控器,可以向上或向下换台(需要按键一次),也可以按一些数字,然后直接跳到该台(需要按键次数等于数字数,不需要按确定键)。但是,这个遥控一些数字键是坏的不能按。问要换到x台最少需要按多少次。x <= 500000。
解法:直接搜索。可能用bfs会快点,但我更喜欢写dfs就用了。
tag:search
// BEGIN CUT HERE
/* */
// END CUT HERE
#line 7 "BrokenButtons.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(x) cout<<x<<":"<<" "
#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 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;} class BrokenButtons
{
public:
int ans, len, p;
bool v[];
void dfs(int x, int num)
{
if (num) ans = min(ans, abs(p-x) + num);
if (num <= len)
for (int i = ; i < ; ++ i) if (!v[i]) dfs (x*+i, num+);
}
int minPresses(int pag, vector <int> bro){
clr0 (v);
for (int i = ; i < sz(bro); ++ i) v[bro[i]] = ;
ans = abs(pag-); p = pag; len = ;
while (pag) pag /= , len ++;
dfs (, );
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 Arr1[] = { , , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, minPresses(Arg0, Arg1)); }
void test_case_1() { int Arg0 = ; int Arr1[] = { , , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, minPresses(Arg0, Arg1)); }
void test_case_2() { int Arg0 = ; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, minPresses(Arg0, Arg1)); }
void test_case_3() { int Arg0 = ; int Arr1[] = {, , , , , , , , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, minPresses(Arg0, Arg1)); }
void test_case_4() { int Arg0 = ; int Arr1[] = { , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, minPresses(Arg0, Arg1)); } // END CUT HERE };
//by plum rain
// BEGIN CUT HERE
int main()
{
//freopen( "a.out" , "w" , stdout );
BrokenButtons ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
DIV1 500pt
题意:有一个数字a[],a的某子数组b[]如果能表示出a,则称b为好子数组。能表示出的意思即为,可以写出如下等式:对任意a[i]均有,a[i] = b[0]*t0 + b[1]*t1 + b[2]*t2 + b[3]*t3 + ... + b[m-1]*t(m-1).(b中有m个元素)。给定a,求它的元素数量最少的好子数组b。
a[i] <= 10^7
解法:首先,从a中选出一些元素形成所求的b,其余元素构成数组b'。则一定有,b中所有元素的最大公约数一定是b'中所有元素的最大公约数的约数,否则无法表示。
所以,为了方便,先将a中所有元素都除以最大公约数,然后在其中找到最少个数,使得找出的数最大公约数为1即可。
其次,注意到,a[i]中每个元素含有质因子的个数最多为8个。(2*3*...23>10^7)
所以,由于b中至少含有一个元素,所以对所有a[i],枚举a[i]在b中的时候,b中最少含有多少个数,问题可以由集合dp得到解决。
tag:math, dp, good
// BEGIN CUT HERE
/* */
// END CUT HERE
#line 7 "BalanceScale.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 clr(x) memset(x, 0, sizeof(x))
#define clrs(x,y) memset(x, y, sizeof(x))
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(x) cout<<x<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, int> pii; 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 d[][<<];
vi divv; class BalanceScale
{
public:
int gao(int sta, int x)
{
int m = sz(divv), ret = ;
for (int i = ; i < m; ++ i) if (sta & (<<i)){
if (x % divv[i] == ) ret |= << i;
}
return ret;
}
int takeWeights(vector <int> w){
sort(all(w));
int gd = w[], n = sz(w);
for (int i = ; i < n; ++ i) gd = __gcd(gd, w[i]);
for (int i = ; i < n; ++ i){
w[i] /= gd;
if (w[i] == ) return ;
}
int ans = n;
for (int i = ; i < n; ++ i){
int tmp = w[i];
divv.clear();
for (int64 j = ; j*j <= tmp; ++ j) if (tmp % j == ){
divv.pb (j);
while (tmp % j == ) tmp /= j;
}
if (tmp != ) divv.pb (tmp); int m = sz(divv);
clr (d); d[][(<<m)-] = ;
for (int j = ; j < n; ++ j){
for (int k = ; k <= j; ++ k)
for (int t = ; t < (<<m); ++ t)
if (d[k][t]) d[k+][gao(t, w[j])] = ;
} for (int j = ; j < n; ++ j) if (d[j][]) ans = min(ans, j+);
}
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(); }
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 Arr0[] = { 5, 4, 1, 8 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(0, Arg1, takeWeights(Arg0)); }
void test_case_0() { int Arr0[] = {, , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, takeWeights(Arg0)); }
void test_case_1() { int Arr0[] = { , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, takeWeights(Arg0)); }
void test_case_2() { int Arr0[] = { , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, takeWeights(Arg0)); }
void test_case_3() { int Arr0[] = { , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, takeWeights(Arg0)); } // END CUT HERE };
//by plum rain
// BEGIN CUT HERE
int main()
{
//freopen( "a.out" , "w" , stdout );
BalanceScale ___test;
___test.run_test(-);
return ;
}
// END CUT HERE