SRM 400(1-250pt, 1-500pt)
DIV1 250pt
题意:给定一个正整数n(n <= 10^18),如果n = p^q,其中p为质数,q > 1,则返回vector<int> ans = {p, q},否则返回ans = {}。
解法:首先,看到题的第一想法是,枚举p。而由于n的范围太大,O(10^9)的复杂度不能接受。又想到,如果题目的限制为q > 2,那么枚举的复杂度就降到了O(10^6),可以接受了。故得到算法,特判n可不可以表示成p ^ 2的形式(注意判定p是不是质数),然后再从1到10^6枚举看n能不能表示成p^q,其中q > 2。这样,问题能够得到解决,但是编码复杂度比较高,最后我写出来只拿到了160+分。
然后看了题解,枚举q...因为10^18 < 2^60,所以q的范围小于60。然后枚举就行了。注意三点,一、枚举的方法可以直接用pow来对n开方,也可以二分,前者会涉及浮点数但是好写。二、枚举的过程中算多少次方不需要快速幂,直接算容易写而且时间复杂度够。三、计算过程中要注意会不会爆long long,如果快爆long long直接返回-1或者0。
tag:math
解法一:
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "StrongPrimePower.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 CLR1(x) memset(x, -1, 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 pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ;
const int maxx = ; int all;
bool vis[maxx];
int prm[maxx]; void sieve(int n)
{
int m = (int)sqrt(n+0.5);
CLR (vis); vis[] = vis[] = ;
for (int64 i = ; i <= m; ++ i) if (!vis[i])
for (int64 j = i*i; j <= n; j += i) vis[j] = ; } int primes(int n)
{
sieve(n);
int ret = ;
for (int i = ; i <= n; ++ i)
if (!vis[i]) prm[ret++] = i;
return ret; } bool ok(int64 n)
{
if (n <= ) return (!vis[n]); for (int i = ; i < all; ++ i)
if ((n % prm[i]) == ){
return ;
}
return ;
} int gao1(int64 n)
{
int64 m = (int64)sqrt(n + 0.5);
if (m*m != n) return -; if (ok(m)) return m;
return -;
} pii gao2(int64 n)
{
int ret;
pii tmp;
for (int i = ; i < all; ++ i) if ((n % prm[i]) == ){
ret = ;
while ((n % prm[i]) == ) {
++ ret;
n /= prm[i];
}
if (ret > && n == ){
tmp.first = prm[i];
tmp.second = ret;
return tmp;
}
else{
tmp.first = -;
return tmp;
}
}
tmp.first = -;
return tmp;
} int64 gao(string n)
{
int64 ret = ;
for (int i = ; i < n.size(); ++ i)
ret = ret * + n[i] - '';
return ret;
} class StrongPrimePower
{
public:
vector <int> baseAndExponent(string N){
all = primes();
int64 n = gao(N); int64 tmp = gao1(n);
vector<int> ans;
ans.clear();
if (tmp != -){
ans.PB ((int)tmp); ans.PB ();
return ans;
}
pii t = gao2(n);
if (t.first != -){
ans.PB (t.first); ans.PB (t.second);
return ans;
}
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(); if ((Case == -) || (Case == )) test_case_5(); }
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 vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
void test_case_0() { string Arg0 = ""; int Arr1[] = {, }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_1() { string Arg0 = ""; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_2() { string Arg0 = ""; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_3() { string Arg0 = ""; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_4() { string Arg0 = ""; int Arr1[] = {, }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_5() { string Arg0 = ""; int Arr1[] = {, }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
StrongPrimePower ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
解法二:
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "StrongPrimePower.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 CLR1(x) memset(x, -1, 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 pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; int64 gao(string n)
{
int64 ret = ;
for (int i = ; i < n.size(); ++ i)
ret = ret * + n[i] - '';
return ret;
} int64 mypow(int64 p, int64 n)
{
unsigned long long ret = ;
for (int i = ; i < n; ++ i){
ret *= p;
if (ret > 1e18) return -;
}
return ret;
} bool ok(int y)
{
for (int64 i = ; i*i <= y; ++ i)
if (y % i == ) return ;
return ;
} int gao(int64 n, int num)
{
int x = (int)pow(n, 1.0 / (double)num), y = -;
for (int i = -; i < ; ++ i)
if (mypow(x+i,num) == n && ok(x+i)) y = x + i;
return y;
} class StrongPrimePower
{
public:
vector <int> baseAndExponent(string N){
int64 n = gao(N);
VI ret; ret.clear();
pii ans;
for (int i = ; i < ; ++ i){
int tmp = gao(n, i);
if (tmp == -) continue;
ret.PB (tmp); ret.PB (i);
}
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(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); }
//void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();}
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 vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
void test_case_0() { string Arg0 = ""; int Arr1[] = {}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_1() { string Arg0 = ""; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_2() { string Arg0 = ""; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_3() { string Arg0 = ""; int Arr1[] = { }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_4() { string Arg0 = ""; int Arr1[] = {, }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); }
void test_case_5() { string Arg0 = ""; int Arr1[] = {, }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, baseAndExponent(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
StrongPrimePower ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
DIV1 500pt
题意:每次翻转操作r(l, r)即是将s中(l, r)的子串反转,比如对abcde进行r(0, 3)即变为cbade。进行一系列翻转变化有一个要求,即r(l1, r1), r(l2, r2), r(l3, r3), r(l4, r4),需l1<=l2<=l3<=l4,r4>=r3>=r2>=r1。要将string s变为string g,求最少所需翻转次数。
解法:就是一个裸DP,但是我对在字符串上的dp好像一窍不通。。。。。什么时候应该挂点字符串dp的专题来刷了。具体数组设置和状态转移方程可以看官方题解,很简单。点击打开官方题解。
tag:字符串,DP
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "ReversalChain.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 CLR1(x) memset(x, -1, 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 pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ;
const int inf = maxint; int d[][][][]; class ReversalChain
{
public:
int minReversal(string s, string g){
CLR (d);
int n = s.size();
for (int i = ; i < n; ++ i)
for (int j = ; j < n; ++ j){
if (s[i] == g[j])
d[][i][j][] = d[][i][j][] = ;
else
d[][i][j][] = d[][i][j][] = inf;
} for (int i = ; i <= n; ++ i)
for (int j = ; j < n; ++ j)
for (int k = ; k < n; ++ k){
d[i][j][k][] = d[i][j][k][] = inf;
if (s[j] == g[k]){
d[i][j][k][] = min(d[i][j][k][], d[i-][j+][k+][]);
d[i][j][k][] = min(d[i][j][k][], d[i-][j+][k+][] + );
}
if (s[j+i-] == g[k+i-]){
d[i][j][k][] = min(d[i][j][k][], d[i-][j][k][]);
d[i][j][k][] = min(d[i][j][k][], d[i-][j][k][] + );
}
if (s[j] == g[k+i-]){
d[i][j][k][] = min(d[i][j][k][], d[i-][j+][k][] + );
d[i][j][k][] = min(d[i][j][k][], d[i-][j+][k][]);
}
if (s[j+i-] == g[k]){
d[i][j][k][] = min(d[i][j][k][], d[i-][j][k+][] + );
d[i][j][k][] = min(d[i][j][k][], d[i-][j][k+][]);
}
}
return d[n][][][] == inf ? - : d[n][][][];
} // 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() { string Arg0 = ""; string Arg1 = ""; int Arg2 = ; verify_case(, Arg2, minReversal(Arg0, Arg1)); }
void test_case_1() { string Arg0 = ""; string Arg1 = ""; int Arg2 = ; verify_case(, Arg2, minReversal(Arg0, Arg1)); }
void test_case_2() { string Arg0 = ""; string Arg1 = ""; int Arg2 = -; verify_case(, Arg2, minReversal(Arg0, Arg1)); }
void test_case_3() { string Arg0 = ""; string Arg1 = ""; int Arg2 = ; verify_case(, Arg2, minReversal(Arg0, Arg1)); }
void test_case_4() { string Arg0 = ""; string Arg1 = ""; int Arg2 = ; verify_case(, Arg2, minReversal(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
ReversalChain ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
SRM 400(1-250pt, 1-500pt)的更多相关文章
- topcoder srm 400 div1
problem1 link 枚举指数,然后判断是不是素数即可. problem2 link 令$f[len][a][b][r]$(r=0或者1)表示子串$init[a,a+len-1]$匹配$goal ...
- SRM475 - SRM479(1-250pt,500pt)
SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)
SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- topcoder srm 553
div1 250pt: 题意:... 解法:先假设空出来的位置是0,然后模拟一次看看是不是满足,如果不行的话,我们只需要关心最后栈顶的元素取值是不是受空白处的影响,于是还是模拟一下. // BEGIN ...
- topcoder srm 552
div1 250pt: 题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个 解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分 ...
- topcoder srm 551
div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...
- topcoder srm 550
div1 250pt: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...
随机推荐
- post和get的区别?
1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...
- javascript基础学习(七)
javascript之Object对象 学习要点: 创建Object对象 Object对象属性 Object对象方法 一.创建Object对象 new Object(); new Object(val ...
- EBS成本核算方法
业务背景 成本核算方法,对应EBS系统中的成本方法,有四种: 1.标准成本 2.平均成本 平均成本又分为永续平均成本,即 Average Cost 期间平均成本,按照期间(自然月)来计算的平均成本 F ...
- 371. Sum of Two Integers -- Avota
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 启动scala的方法
1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...
- 明解C语言,练习13-3,从文件中读入个人信息,按身高排序后显示
#include <stdio.h> #define NUMBER 6 #define F_PATH "D:\\C_C++\\ec13-3\\hw.dat" typed ...
- nginx插件ngx_lua
ngx_lua是淘宝的维护的产品,真心不错.配置文件包含可以做很多事情的lua脚本. 公司有个产品对注册的广告盒子进行反向代理,这样可以在盒子上做很多事情:和服务器通信,远程控制盒子等等.nginx反 ...
- nginx location
1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何前缀:最大前缀匹配,举例如 ...
- xml 个人练习2
package cn.gdpe.xml; import java.io.File;import java.io.FileInputStream;import java.io.IOException;i ...
- JS设置Cookie,及COOKIE的限制
在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...