SRM471-創(chuàng)新互聯(lián)

250pt:SRM471

 題意:定義一種函數(shù)f(x),表示x不斷/2直到出現(xiàn)非素數(shù)的操作次數(shù)。現(xiàn)在給定N,D。求X<= N, 并且f(x) >= D的大的數(shù)

我們提供的服務(wù)有:網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、新羅ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的新羅網(wǎng)站制作公司

 思路:直接先弄一個1000w以內(nèi)的質(zhì)數(shù)表,然后直接dp。由于題目給定內(nèi)存才64M。。所以沒法開1000w的int數(shù)組

    所以dp時我直接對每個素數(shù)做。dp[i]表示以第i個質(zhì)數(shù)結(jié)尾的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;
typedeflong long LL;
typedef pair<int,int> PII;
bool vis[10000010];
vector<int> P;
int dp[1000000];
class PrimeSequences
{
public:
void getPrime(int n){
              P.clear();
for (int i = 2; i <= n; ++i){
if (vis[i]) continue;
                   P.push_back(i);
for (int j = i * 2;  j <= n; j += i)
                      vis[j]= true;
              }

        }
int getLargestGenerator(int N, int D)
        {
                 getPrime(N);
int n = P.size();
int p, x;
int ans = 0;
for (int i = 0; i < n; ++i){
                       x= (P[i] >> 1);
                       p= lower_bound(P.begin(), P.end(), x) - P.begin();
if (x == P[p]) dp[i] = dp[p] + 1;
else dp[i] = 1;
                       ans= max(dp[i], ans);
                 }
if (ans < D) return -1;
for (int i = n-1; i >= 0; --i)
if (dp[i] >= D) return P[i];
return -1;
        }

};

500pt:

題意:題目給了最多n(n <= 25)個點的有向圖,編號0~n-1, 現(xiàn)在求一條0號點到n-1點的最短路,并且該最短路上任意兩點距離不為13倍數(shù)。

思路:很明顯的動態(tài)規(guī)劃。

    后面改成3維dp[i][j][k],表示到達點i,從起點到點i所有路徑%13的集合為j,并且此時最短路%13為k時的最短路。

    那么轉(zhuǎn)移就很明顯了。。

    不過此題很容易想入非非了,剛開始就是用2維寫錯了。少枚舉了第三維。直接用最短路%13作為第三維。。

    這樣的結(jié)果就可能導(dǎo)致當(dāng)前最優(yōu)而后面不一定最優(yōu)。。

code:

 1 #line 7 "ThirteenHard.cpp"
 2 #include <cstdlib>
 3 #include <cctype>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <string>
10 #include <iostream>
11 #include <sstream>
12 #include <map>
13 #include <set>
14 #include <queue>
15 #include <stack>
16 #include <fstream>
17 #include <numeric>
18 #include <iomanip>
19 #include <bitset>
20 #include <list>
21 #include <stdexcept>
22 #include <functional>
23 #include <utility>
24 #include <ctime>
25 using namespace std;
26 
27 #define PB push_back
28 #define MP make_pair
29 #define Inf 0xfffffff
30 #define REP(i,n) for(i=0;i<(n);++i)
31 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
32 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
33 #define two(i) (1 << i)
34 typedef vector<int> VI;
35 typedef vector<string> VS;
36 typedef vector<double> VD;
37 typedef long long LL;
38 typedef pair<int,int> PII;
39 struct oo{
40   int x, y, z;
41        oo(){}
42        oo(int _x, int _y, int _z):x(_x), y(_y), z(_z){}
43 };
44 int dp[26][1 << 14][15];
45 bool vis[26][1 << 14][15];
46 class ThirteenHard
47 {
48 public:
49 int g[30][30], n;
50 int calcTime(vector <string> s)
51         {
52              n = s.size();
53   for (int i = 0; i < n; ++i)
54   for (int j = 0; j < n; ++j){
55  if (s[i][j] == '#') g[i][j] = Inf;
56  if (s[i][j] >= 'A' && s[i][j] <= 'Z') g[i][j] = s[i][j] - 'A' + 1;
57  if (s[i][j] >= 'a' && s[i][j] <= 'z') g[i][j] = s[i][j] - 'a' + 27;
58                 }
59              memset(dp, -1, sizeof(dp));
60              memset(vis, 0, sizeof(vis));
61              dp[0][1][0] = 0;
62              queue<oo> q;
63              q.push(oo(0,1,0));
64   int x, y, z;
65              vis[0][1][0] = true;
66              oo cur;
67   while (!q.empty()){
68                    cur = q.front();
69   for (int i = 0; i < n; ++i) if (g[cur.x][i] < Inf){
70                             z = (cur.z + g[cur.x][i]) % 13;
71   if (two(z) & cur.y) continue;
72                             y = (cur.y | two(z));
73                             x = i;
74   if (dp[x][y][z] == -1 || dp[x][y][z] > dp[cur.x][cur.y][cur.z] + g[cur.x][x]){
75                                   dp[x][y][z] = dp[cur.x][cur.y][cur.z] + g[cur.x][x];
76   if (!vis[x][y][x]){
77                                        vis[x][y][z] = true;
78                                        q.push(oo(x, y, z));
79                                   }
80                             }
81                    }
82                    q.pop();
83                    vis[cur.x][cur.y][cur.z] = false;
84              }
85   int ans = -1;
86   for (int i = 0; i < two(13); ++i)
87 for (int j = 0; j < 13; ++j)
88 if (dp[n-1][i][j] > -1)
89 if (ans == -1 || dp[n-1][i][j] < ans) ans = dp[n-1][i][j];
90   return ans;
91         }
92 
93 };
View Code

網(wǎng)頁名稱:SRM471-創(chuàng)新互聯(lián)
文章分享:http://bm7419.com/article46/dicgeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站營銷、定制網(wǎng)站商城網(wǎng)站、ChatGPT、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名