SRM 358(1-250,500pt)
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
SRM 358(1-250,500pt)的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- topcoder srm 628 div2 250 500
做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: #i ...
- SRM 595 DIV1 250
挺简单的组合把. #include <cstdio> #include <cstring> #include <iostream> #include <vec ...
- SRM 594 DIV1 250
可能开始宿舍比较乱,思绪静不下来...想了大半个小时,终于确定了应该暴力+DP,然后写了枚举除数,和被除的版本..这样,还敲错了个字母,第一次提交还100多,修改提交还有75.多,最后想到,貌似不打对 ...
- TC SRM 593 DIV1 250
我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...
- TC SRM 593 DIV1 250(dfs)
这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- topcoder srm 610 div2 250
第一次做tc 的比赛,一点也不懂,虽然题目做出来了, 但是,也没有在比赛的时候提交成功.. 还有,感谢一宁对tc使用的讲解.. 贴一下代码..... #include <cstring> ...
- 最小公倍数 SRM 661 Div1 250: MissingLCM
Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...
随机推荐
- go build 时报错 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
最近在玩Go win下尝试编译Go的时候遇到了下面提示(可能是gorocksdb用到了gcc) gcc也需要64位的 最后找到了个帖子: https://github.com/mattn/go-sql ...
- CentOS 配置Apache+Mysql+PHP (yum)与卸载
一.安装Apache2 #yum -y install httpd 安装配置完成,启动httpd服务#service httpd start 二.安装Mysql1.安装mysql#yum -y ins ...
- wcf通道Channel
正文 客户端与服务进行交互的过程是通过通道进行交互的.客户端通过调用代理类执行相应的方法,通过通道编码,调用上下文,传输客户端的事务,管理可靠会话,对消息正文的加密,最后要执行的通道是传输通道就像我们 ...
- JS进制转换,浮点数相加,数字判断
document.write("整数转换函数:parseInt(数据,底数)<br>"); document.write("10101=>" ...
- CentOS使用sudo提示用户不在sudoers文件中的解决方法
1切换到root用户[linux@localhost ~]$ su root密码:[root@localhost ~]# 2查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 [roo ...
- JavaScript 排序算法——快速排序
常见排序 javaScript 实现的常见排序算法有:冒泡排序.选择排序.插入排序.谢尔排序.快速排序(递归).快速排序(堆栈).归并排序.堆排序. 过程 "快速排序"的思想很简单 ...
- delphi xe5 android sample
安装xe5以后demo存放的路径在 C:\users\Public\Documents\RAD Studio\12.0\Samples 另外易博龙在sourceforget上也有 svn地址为:sv ...
- POJ2103 Jackpot
Description The Great Dodgers company has recently developed a brand-new playing machine. You put a ...
- Emily姨妈家的猫
按书上的样例,慢慢理解. 其实,JAVASCRIPT也应该可以写出正规点的,封装性好的代码. <html> <body> <script type="text/ ...
- Linux下实现定时器Timer的几种方法
http://blog.csdn.net/lxmky/article/details/7669296 第六章 IO复用:select和poll函数 http://www.cnblogs.com/4ti ...