250pt:

题意:定义一种函数f(x),表示x不断/2直到出现非素数的操作次数。现在给定N,D。求X<= N, 并且f(x) >= D的最大的数

思路:直接先弄一个1000w以内的质数表,然后直接dp。由于题目给定内存才64M。。所以没法开1000w的int数组

所以dp时我直接对每个素数做。dp[i]表示以第i个质数结尾的f值。。

code:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std; #define PB push_back
#define MP make_pair #define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
bool vis[];
vector<int> P;
int dp[];
class PrimeSequences
{
public:
void getPrime(int n){
P.clear();
for (int i = ; i <= n; ++i){
if (vis[i]) continue;
P.push_back(i);
for (int j = i * ; j <= n; j += i)
vis[j] = true;
} }
int getLargestGenerator(int N, int D)
{
getPrime(N);
int n = P.size();
int p, x;
int ans = ;
for (int i = ; i < n; ++i){
x = (P[i] >> );
p = lower_bound(P.begin(), P.end(), x) - P.begin();
if (x == P[p]) dp[i] = dp[p] + ;
else dp[i] = ;
ans = max(dp[i], ans);
}
if (ans < D) return -;
for (int i = n-; i >= ; --i)
if (dp[i] >= D) return P[i];
return -;
} };

500pt:

题意:题目给了最多n(n <= 25)个点的有向图,编号0~n-1, 现在求一条0号点到n-1点的最短路,并且该最短路上任意两点距离不为13倍数。

思路:很明显的动态规划。

后面改成3维dp[i][j][k],表示到达点i,从起点到点i所有路径%13的集合为j,并且此时最短路%13为k时的最短路。

那么转移就很明显了。。

不过此题很容易想入非非了,刚开始就是用2维写错了。少枚举了第三维。直接用最短路%13作为第三维。。

这样的结果就可能导致当前最优而后面不一定最优。。

code:

 #line 7 "ThirteenHard.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std; #define PB push_back
#define MP make_pair
#define Inf 0xfffffff
#define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)
#define two(i) (1 << i)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
struct oo{
int x, y, z;
oo(){}
oo(int _x, int _y, int _z):x(_x), y(_y), z(_z){}
};
int dp[][ << ][];
bool vis[][ << ][];
class ThirteenHard
{
public:
int g[][], n;
int calcTime(vector <string> s)
{
n = s.size();
for (int i = ; i < n; ++i)
for (int j = ; j < n; ++j){
if (s[i][j] == '#') g[i][j] = Inf;
if (s[i][j] >= 'A' && s[i][j] <= 'Z') g[i][j] = s[i][j] - 'A' + ;
if (s[i][j] >= 'a' && s[i][j] <= 'z') g[i][j] = s[i][j] - 'a' + ;
}
memset(dp, -, sizeof(dp));
memset(vis, , sizeof(vis));
dp[][][] = ;
queue<oo> q;
q.push(oo(,,));
int x, y, z;
vis[][][] = true;
oo cur;
while (!q.empty()){
cur = q.front();
for (int i = ; i < n; ++i) if (g[cur.x][i] < Inf){
z = (cur.z + g[cur.x][i]) % ;
if (two(z) & cur.y) continue;
y = (cur.y | two(z));
x = i;
if (dp[x][y][z] == - || dp[x][y][z] > dp[cur.x][cur.y][cur.z] + g[cur.x][x]){
dp[x][y][z] = dp[cur.x][cur.y][cur.z] + g[cur.x][x];
if (!vis[x][y][x]){
vis[x][y][z] = true;
q.push(oo(x, y, z));
}
}
}
q.pop();
vis[cur.x][cur.y][cur.z] = false;
}
int ans = -;
for (int i = ; i < two(); ++i)
for (int j = ; j < ; ++j)
if (dp[n-][i][j] > -)
if (ans == - || dp[n-][i][j] < ans) ans = dp[n-][i][j];
return ans;
} };

SRM471的更多相关文章

随机推荐

  1. Github远程仓库关联

    一.Git的安装 1.git的安装和配置 (1)配置用户名和邮箱,如下所示: $ git config --global user.name [username] $ git config --glo ...

  2. *jquery操作DOM总结 (原创:最全、最系统、实例展示)

    jquery操作DOM包括八个方面: 一:jquery对DOM节点的基本操作:二:jquery对DOM节点的CSS样式操作:三:jquery遍历DOM节点:四:jquery创建DOM节点:五:jque ...

  3. jQuery操作(二)

    一: 操作元素 1. 属性操作 1.1 $("p").text() $("p").html() $(":checkbox").val() 1 ...

  4. How to use external classes and PHP files in Laravel Controller?

    By: Povilas Korop Laravel is an MVC framework with its own folder structure, but sometimes we want t ...

  5. 移动端300ms延迟由来及解决方案

    1.300ms延迟由来 300 毫秒延迟的主要原因是解决双击缩放(double tap to zoom).双击缩放,顾名思义,即用手指在屏幕上快速点击两次,iOS 自带的 Safari 浏览器会将网页 ...

  6. 【Ruby】ruby安装

    Ruby简介 Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License.它 ...

  7. make/makefile中的加号+,减号-和at号@的含义

    http://www.crifan.com/order_make__makefile_in_the_plus__minus_-_and_at_the_meaning_of_numbers/ 在看mak ...

  8. 团队-Python 爬取豆瓣电影top250-成员简介及分工

    姓名:周鑫 班级:软件6班 团队名称:咣咣踹电脑 擅长:Python,java 分工:编写数据库

  9. kbmmw 中XML 操作入门

    delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使 ...

  10. 2018.10.31 NOIP训练 锻造(方程式期望入门题)(期望dp)

    传送门 根据题目列出方程: fi=pi∗(fi−1+fi−2)+(1−pi)∗(fi+1+fi)f_i=p_i*(f_{i-1}+f_{i-2})+(1-p_i)*(f_{i+1}+f_i)fi​=p ...