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)的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. topcoder srm 628 div2 250 500

    做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: #i ...

  4. SRM 595 DIV1 250

    挺简单的组合把. #include <cstdio> #include <cstring> #include <iostream> #include <vec ...

  5. SRM 594 DIV1 250

    可能开始宿舍比较乱,思绪静不下来...想了大半个小时,终于确定了应该暴力+DP,然后写了枚举除数,和被除的版本..这样,还敲错了个字母,第一次提交还100多,修改提交还有75.多,最后想到,貌似不打对 ...

  6. TC SRM 593 DIV1 250

    我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...

  7. TC SRM 593 DIV1 250(dfs)

    这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  8. topcoder srm 610 div2 250

    第一次做tc 的比赛,一点也不懂,虽然题目做出来了, 但是,也没有在比赛的时候提交成功.. 还有,感谢一宁对tc使用的讲解.. 贴一下代码..... #include <cstring> ...

  9. 最小公倍数 SRM 661 Div1 250: MissingLCM

    Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...

随机推荐

  1. python 自动化之路 day 07 面向对象基础

    本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.   面向对象编程 OOP编程是利用"类"和"对象" ...

  2. js hover放大效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. php static 关键字在 函数中的用法

    至于在类中声明为 static 的属性和方法,这里不描述php中的变量作用范围的另一个重要特性就是静态变量(static 变量).静态变量仅在局部函数域中存在且只被初始化一次,当程序执行离开此作用域时 ...

  4. dom例子

    //凡是html标签中的属性和值是一样的,那么在js中用true或者false 1,阅读协议倒计时 <input type="button" name="name& ...

  5. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  6. 从小白进阶ubuntu高手的必经之路—命令

    精选的十二个ubuntu下的命令,熟记于心,则能甩掉ubuntu小白标签,高手的伟岸形象焕然生发.一.管理员权限绝大部分情况下,命令的行为须要被赋予管理员权限才能执行.命令 sudo 作用:赋予当前命 ...

  7. 第 10 章 建造者模式【Builder Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 又是一个周三,快要下班了,老大突然又拉住我,喜滋滋的告诉我“牛叉公司很满意我们做的模型,又签订了一个合同,把奔驰.宝马的 ...

  8. How to new a screen in linux

    screen -R -D: create a screen Ctrl + A & Ctrl + D: leave a screen

  9. ArrayList与Vector、HashMap与HashTable

    摘自api: 1.ArrayList与Vector: 原文:This class(ArrayList) is roughly equivalent to Vector, except that it ...

  10. SpringSecurity的简单应用(一)

    java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downlo ...