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 ...
随机推荐
- C++输入输出缓冲区的刷新问题
当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起.这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介.例如,对于一个输出 ...
- mui H5 js动态添加不同类型的数据
html页面需要添加的页面的数据格式 <ul class="mui-table-view" id="OA_task_1"> <li class ...
- php 遍历一个文件夹下的所有文件和子文件夹
<?php function my_scandir($dir) { $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { ...
- 大话设计模式(带目录完整版).pdf等
点击进入百度网盘 大话设计模式(带目录完整版).pdf等 保存至网盘下载二维码举报 赞(0)| 评论 | 分享至 分享时间:2014-04-01 11:02 | 467次浏览 130次下载 81次保存 ...
- CodeFirst数据库迁移小记
打开“程序包管理器控制台”菜单项一.Enable-Migrations -ContextTypeName Code_First_数据迁移.Models.T_DbContext成功后提示:已在项目“Co ...
- CommonsChunkPlugin的一些总结
CommonsChunkPlugin 官方文档地址 https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin new ...
- 【转载】Using the Web Service Callbacks in the .NET Application
来源 This article describes a .NET Application model driven by the Web Services using the Virtual Web ...
- 10 个 jQuery 的无限滚动的插件:
很多社交网站都使用了一些新技术来提高用户体验,而无限滚动的翻页技术就是其中一项,当你页面滑到列表底部时候无需点击就自动加载更多的内容. 下面为你推荐 10 个 jQuery 的无限滚动的插件: 1. ...
- Android使用Dom解析xml
一.理论准备 二.上代码 <?xml version="1.0" encoding="utf-8" ?> < ...
- Penetration test
Contents 1 History 2 Standards and certification 3 Tools 3.1 Specialized OS distributions 3.2 Softwa ...