SRM 597
我果然是题短了就能做得好- -。Div 2的三道题都短,于是迅速的过掉了250和500,rating涨了150^ ^。
Div 2 250pt
题意:给一个vector<int> A,对其中任意的数x,可以将其变为2^t * x(t为正整数),判断能不能将A变为所有数全部一样。
解法:我的方法是,将A排序,对第i个,while(A[i] < A[i+1]) A[i] *= 2,如果最终A[i] != A[i+1],则答案为不能。若对所有的i都可以,则答案为能。
水题就不贴代码了。
Div 2 500pt/ Div 1 250pt
题意:给两个等长的字符串s1和s2,对s1进行操作,每次操作能将s1中某个字符提前到s1中的第一个。问最少进行多少次操作,能将s1变成s2。若不能返回-1。
解法:首先,对s1和s2所含字符的数量进行统计,判断能不能将s1变为s2。
然后,改变后的s1有两种字符,被操作过的字符全部在s1的前部分,没被操作过的字符全部在s1的后部分,而且,对于后部分的字符,相对顺序在s1改变前后并没有改变。
所以,从后往前用i枚举s1的所有字符,同时置一个idx对s2从后往前移动。if (s1[i] == s2[idx]) {-- i; -- idx} else -- i;
这样,idx+1即为答案。
tag:greedy
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "LittleElephantAndString.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back char a[], b[];
int num[][]; class LittleElephantAndString
{
public:
int getNumber(string A, string B){
char x = 'A'; int n = A.size();
for (int i = , j = n-; j >= ; ++ i, -- j){
a[i] = A[j];
b[i] = B[j];
} CLR (num);
for (int i = ; i <= n; ++ i){
num[a[i]-x][] ++;
num[b[i]-x][] ++;
} for (int i = ; i < ; ++ i)
if (num[i][] != num[i][]) return -; int cnt = n - ;
for (int i = n-; i >= ; -- i){
if (A[i] == B[cnt])
-- cnt;
}
if (cnt == n-) return -;
return cnt+;
}
};
Div2 1000pt
题意:给定一个数n,称这样的集合为关于n的好集合:所有元素的值均在n以内,且将所有元素写在黑板上,所需要写的0, 1, 2..9的次数均不超过1次。给定n求好数组的数量。(n <= 10^9),答案关于1000000007取模。
解法:对于任意给定的数n,首先用dfs求出所有能被放在关于n的好集合中的数,并将他们分好类。分类的方法为,对于数k,将它写在黑板上需要写的数字有a0, a1, a2..at,则它属于类((0 | 1<<a0) | (1<<a1) | ... | (1<<at))。比如1属于类2,10属于类3,123属于类14。
分好类之后,就发现,同一个集合之中,最多一个能出现在关于n的好集合中,且不同集合的数也不一定能同时出现在关于n的好集合中,比如类2和类3(因为都含有1)。
这样的话,就变成了一个01背包问题。设d[i][j]表示前i类中去数,状态为j的情况下能有多少好集合。
注意到,此处第i类所代表的状态也为i,所以状态转移方程为d[i][i|j] += d[i-1][j] * num(类i)。num(类i)表示类i中含有元素的个数。
tag:dp, 背包, good
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "LittleElephantAndSubset.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 CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
//#define SZ(v) ((int)(v).size())
//#define zero(x) (((x)>0?(x):-(x))<eps)
//#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-8;
//const double PI = atan(1.0)*4;
//const int maxint = 2139062143;
const int64 mod = ;
// int n, num;
bool vis[], opt[];
int64 d[<<];
vector<int> a[]; int64 max64(int64 a, int64 b)
{
return a > b ? a : b;
} void dfs(int x, int p)
{
int64 y = (int64)x * + p;
if (y > n) return; int sta = ;
for (int i = ; i <= ; ++ i)
if (vis[i]) sta |= ( << i);
a[sta].PB((int)y); for (int i = ; i <= ; ++ i) if(!vis[i]){
vis[i] = ;
dfs((int)y, i);
vis[i] = ;
}
} bool ok(int a, int b)
{
for (int i = ; i < ; ++ i){
int t1 = a & ( << i), t2 = b & ( << i);
if (t1 && t2) return ;
}
return ;
} class LittleElephantAndSubset
{
public:
int getNumber(int N){
n = N; num = << ;
for (int i = ; i < num; ++ i)
a[i].clear();
CLR (vis); for (int i = ; i <= ; ++ i){
vis[i] = ;
dfs(, i);
vis[i] = ;
} CLR (d);
for (int i = ; i < num; ++ i){
if (a[i].size()) d[i] = (a[i].size() + d[i]) % mod;
for (int j = num-; j >= ; -- j) if (ok(i, j))
d[i|j] = ((d[j]%mod) * ((int64)a[i].size()%mod) + d[i|j]) % mod;
} int ret = ;
for (int i = ; i < num; ++ i)
ret = (d[i] + ret) % mod;
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(); }
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 = ; verify_case(, Arg1, getNumber(Arg0)); }
void test_case_1() { int Arg0 = ; int Arg1 = ; verify_case(, Arg1, getNumber(Arg0)); }
void test_case_2() { int Arg0 = ; int Arg1 = ; verify_case(, Arg1, getNumber(Arg0)); }
void test_case_3() { int Arg0 = ; int Arg1 = ; verify_case(, Arg1, getNumber(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
freopen( "a.out" , "w" , stdout );
LittleElephantAndSubset ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
SRM 597的更多相关文章
- TC SRM 597 DEV2
第一次玩TC的SRM,只完成了一题,有点失落,不过还是要把每个问题都研究清楚才是我的本性,呵呵 第一题思路: 任意一个数,不断除掉2以后的剩下的数若相同则YES否则NO 第二题: 最开始判断字母个数是 ...
- Topcoder SRM 597
妈蛋第一场tc就掉分,提交了第一个题的时候就注定悲剧要发生了,妈蛋没考虑0就直接%了,真的是人傻见识又少,第二题最后有了一点思路,没时间写了,可能也不是很准确,第三题想了小会儿效果为0! 然后第一题傻 ...
- Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1
据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
- SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)
SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...
- SRM 657 DIV2
-------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...
- nyist 597 完数?
http://acm.nyist.net/JudgeOnline/problem.php?pid=597 完数? 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 一个 ...
- SRM DIV1 500pt DP
SRM 501 DIV1 500pt SRM 502 DIV1 500pt SRM 508 DIV1 500pt SRM 509 DIV1 500pt SRM 511 DIV1 500pt SRM 5 ...
随机推荐
- C语言字符串库函数的实现
1.strlen(字符串的长度) size_t Strlen(const char* str) { assert(str); ;; ++i) { if (str[i] == '\0') return ...
- POJ刷题记录 (。・`ω´・)(Progress:6/50)
1743:前后作差可以转化成不可重叠最长公共字串问题,运用后缀数组解决(参考罗穗骞神犇的论文) #include <cstdio> #include <cstring> #in ...
- 深度探索va_start、va_arg、va_end
采用C语言编程的时候,函数中形式参数的数目通常是确定的,在调用时要依次给出与形式参数对应的所有实际参数.但在某些情况下希望函数的参数个数可以根据需要确定.典型的例子有大家熟悉的函数printf().s ...
- 【HDU2815】【拓展BSGS】Mod Tree
Problem Description The picture indicates a tree, every node has 2 children. The depth of the nod ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- 用urlencode(String str)对URL传递参数进行编码,提高安全
在PHP 提交地址后面带有参数的时候,参数会在浏览器的地址栏暴露无疑,这样是不安全的,这个时候就必须用些方法对这些参数进行安全处理 这里可以用 urlencode(String URL);//对URL ...
- python自动开发之第十三天
1.Paramiko模块下的demo.py程序 前面利用Python中的Paramiko模块可以进行SSH的连接,以及用来传送文件(SFTP),但是无论是哪一种方式,连接都是短暂的,并非是长连 ...
- Insert Interval 面试题leetcode.
刚开始做这个题的时候绕了好大的圈,对问题的分析不全面,没能考虑所有情况,做的很纠结.后来看了下大神的做法很受启发,改了改代码,最终提交了. public static ArrayList<Int ...
- asp.net 导入
开发项目过程中会遇到各种各样的项目需求,我现在遇到的问题是每个部门有不同的excel文件类型,他们每个部门每个文件类型上传成功之后都会在数据库中产生表,表的列名是你excel第一行数据,其他行作为表的 ...
- BZOJ 1021 循环的债务
Description Alice.Bob和Cynthia总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在清还债务的 ...