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. Linux CentOS 7 & JDK 1.7 安装与配置

    前言 简单记录一下在CentOS 7中安装配置JDK 1.7的全过程~ 下载 首先是jdk 1.7 64bit & 32bit的下载地址: jdk-7u79-linux-x64.tar.gz ...

  2. SoftwareEngineering.APIDesign.iOS

    API Design for iOS/Mac (Objective-c Edition) 1. UI Control Library API的设计 和已有组件保持一致(例如: 使用标准的API, 模型 ...

  3. python多线程下载网页图片并保存至特定目录

    #!python3 #multidownloadXkcd.py - Download XKCD comics using multiple threads. import requests impor ...

  4. 品味性能之道<九>:利用Loadrunner编写socket性能测试脚本简述

            一.概述         Loadrunner拥有极为丰富的工具箱,供予我们制造出各种奇妙魔法的能力.其中就有此次要讨论的socket套接字操作.     二.socket概述     ...

  5. Python pip下载安装库 临时用清华镜像命令

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple C:\Users\mu\pip 新建pip.ini [global] index-url ...

  6. lodash 中常用的方法

    odash是js集Array/Object/String/Function的Util于一身. lodash打包了Array/Object/String/Function里一些Api,好处是连ES6的也 ...

  7. spring.boot mybaits集成

    https://www.cnblogs.com/pejsidney/p/9272562.html (insertBatch批量插入) 第一篇博客循环部分有错误,参照下面的例子去更改 List<S ...

  8. @Valid报错 No validator could be found for constraint

    使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  9. docker 批量操作容器

    docker stop $(sudo docker ps -q)

  10. 【转】【MySQL】时间类型存储格式选择

    一  前言  昨天在给开发同学做数据库设计规范分享的时候,讲到时间字段常用的有三个选择datetime.timestamp.int,应该使用什么类型的合适?本文通过三种类型的各个维度来分析,声明:本文 ...