銆愬仛棰樸€慳rc080_f-Prime Flip鈥斺€旇浆鎹€佹暟璁哄強鍖归厤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愬仛棰樸€慳rc080_f-Prime Flip鈥斺€旇浆鎹€佹暟璁哄強鍖归厤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/can' title='can'>can
return fir display str exp oid 璐ㄦ暟 瀹炵幇涓綅缃笂鐨勬暟涓?span class="math inline">(1)锛屽叾浣欓兘鏄?span class="math inline">(0)銆備綘鍙互杩涜鑻ュ共娆℃搷浣滐紝姣忔閫夊彇搴忓垪涓婄殑涓€涓尯闂?span class="math inline">([l,r))锛屾弧瓒?span class="math inline">(r-l)涓哄璐ㄦ暟锛屽皢鍦ㄨ繖涓尯闂翠笂鐨勬暟閮藉紓鎴栦笂(1)銆傞棶鏈€灏戣繘琛屽灏戞鎿嶄綔锛屼娇寰楀簭鍒椾笂鎵€鏈夋暟閮藉彉涓?span class="math inline">(0)銆?/p>棰樻剰锛氭湁涓€涓棤闄愬簭鍒楋紝鍏朵腑鏈?span class="math inline">(n)
(n leq 100)
杩欑被鑷繁鍐冲畾鎿嶄綔鏉ヨ鏁板垪鍙樹负鍏ㄩ浂鐨勯棶棰橈紝鍦╝tcoder涓婃槸鎸哄父瑙佺殑銆?/p>
涓€鑸浜庤繖绉嶉棶棰橈紝瑕佺敤宸垎鎴栫被浼肩殑鎿嶄綔锛屾潵璁╁尯闂翠慨鏀瑰彉涓哄崟鐐逛慨鏀广€備緥濡傦紝瀵逛簬鍖洪棿鍔犵殑鎿嶄綔锛屽氨鐢ㄥ樊鍒嗗皢鍏跺彉涓轰袱涓崟鐐瑰姞銆傝嚦浜庤繖涓棶棰橈紝鎴戜滑璁惧師搴忓垪涓烘暟鍒?span class="math inline">({a})
锛屽垯鏋勫缓鏁板垪({b})婊¤冻(b_n=a_{n-1} ,{ m xor} ,a_n)銆傝繖鏍凤紝棰樼洰涓殑([l,r))鍖洪棿寮傛垨(1)灏卞彉鎴愪簡灏?span class="math inline">(b_l)鍜?span class="math inline">(b_r)閮藉紓鎴栦笂(1)銆?/p>鎺ヤ笅鏉ワ紝鎴戜滑鑰冭檻鎿嶄綔鍙互琚垎涓哄嚑涓儴鍒嗭紝姣忎竴涓儴鍒嗗氨鏄妸涓や釜涓?span class="math inline">(1)
鐨勫厓绱犲彉涓?span class="math inline">(0)锛屽叾浣欏厓绱犲潎涓嶆敼鍙樼姸鎬併€備簬鏄紝闂灏卞彉鎴愪簡瀵?span class="math inline">({b})涓负(1)鐨勫厓绱犺繘琛屽尮閰嶃€傦紙鍙互鍙戠幇锛屼负(1)鐨勫厓绱犵殑鎬绘暟涓哄伓鏁帮級鎴戜滑鑰冭檻涓や釜浣嶇疆鐨勮窛绂讳负(d)鐨勫厓绱狅紝灏嗕粬浠兘鍙樹负(0)锛岄渶瑕佸灏戞鎿嶄綔銆傝繖绛変环浜庢妸(d)琛ㄧず涓烘渶灏戞暟閲忕殑璐ㄦ暟鐨勫拰鎴栧樊銆?/p>
- (d)涓哄璐ㄦ暟銆傞偅涔堬紝鏄剧劧涓€娆℃搷浣滃氨鍙互浜嗐€?/li>
- (d)涓哄伓鏁般€傞鍏堬紝浠庡鍋舵€т笂鐪嬶紝鍙敤涓€娆℃搷浣滄槸涓嶅彲鑳界殑銆傛晠鑷冲皯闇€瑕佷袱娆℃搷浣溿€傝€冭檻(d=2)鏃讹紝鍙互鐢?span class="math inline">(2=5-3)
鍓╀笅鐨勫氨鏄尮閰嶇殑闂浜嗐€傝繖閲屼笉鑳芥媶鐐瑰仛浜屽垎鍥惧甫鏉冩渶澶у尮閰嶏紝寰堝鏄撹兘涓惧嚭鍙嶄緥銆備絾鏄紝鎴戜滑鍙互鐢ㄨ椽蹇冩潵鎶婃墍鏈夊厓绱犲垎涓轰袱缁勩€傦紙姝ゅ鐬勪簡棰樿В锛夎(k)涓烘墍鏈夊尮閰嶄腑(d)涓哄璐ㄦ暟鐨勭粍鏁般€傞偅涔堬紝鏄剧劧鏄?span class="math inline">(k)
缁勫鏁颁綅缃拰鍋舵暟浣嶇疆鍖归厤銆傝濂囨暟鐨勪綅缃笂鏈?span class="math inline">(n_1)涓厓绱狅紝鍋舵暟鐨勪綅缃笂鏈?span class="math inline">(n_2)涓厓绱犮€傞偅涔堬紝鏄剧劧瑕佽濂囧伓鎬х浉鍚岀殑鍏冪礌鍖归厤鍒颁竴璧凤紝鎿嶄綔鎬绘暟鏈€灏忋€備簬鏄紝鎴戜滑鍙互寰楀嚭锛屾€绘搷浣滄暟涓?span class="math display">[k+2 imes (lfloor frac {n_0-k} {2} floor+lfloor frac {n_1-k} {2} floor) + 3 imes ((n_0 - k) mod 2)]鐢变簬鍏冪礌鎬绘暟涓哄伓鏁帮紝(n_0)鍜?span class="math inline">(n_1)
鍚屽鍋讹紝鎴戜滑鍒嗗鍋惰璁轰竴涓嬪氨鑳藉緱鍒板浜庢墍鏈夊悎娉曠殑(k)鍜?span class="math inline">(k+1)锛?span class="math inline">(f(k+1) leq f(k))銆傚洜姝わ紝鎴戜滑鍙鏈€澶у寲(k)灏卞彲浠ヤ簡銆傛垜浠牴鎹鍋舵€у鍏冪礌鍒嗙粍锛岀劧鍚庡仛浜屽垎鍥炬渶澶у尮閰嶅氨鍙互浜嗐€?/p>鏃堕棿澶嶆潅搴?span class="math inline">(O(n^2 + m))
銆?span class="math inline">(m)涓烘潈鍊煎ぇ灏忋€?/p>#include <bits/stdc++.h>
using namespace std;
const int N = 610, INF = 0x3f3f3f3f;
int p[N],n,cnt,st,en,x[N];
struct edge {
int la,b,cap;
} con[N * N * 2];
int tot=1,fir[N];
void add(int from,int to,int capc) {
con[++tot] = (edge) {fir[from],to,capc};
fir[from] = tot;
con[++tot] = (edge) {fir[to],from,0};
fir[to] = tot;
}
int cur[N], dis[N];
int dfs(int pos,int imp) {
if (pos == en || (!imp)) return imp;
int expo = 0, tmp;
for (int &i = cur[pos] ; i ; i = con[i].la) {
if (dis[con[i].b] == dis[pos] + 1) {
tmp = dfs(con[i].b,min(imp,con[i].cap));
con[i].cap -= tmp;
con[i^1].cap += tmp;
expo += tmp;
imp -= tmp;
if (!imp) break;
}
}
return expo;
}
bool bfs() {
static queue<int> q;
while (!q.empty()) q.pop();
memset(dis,0,sizeof dis);
for (int i = 1 ; i <= n ; ++ i)
cur[i] = fir[i];
dis[st] = 1;
q.push(st);
for (int pos ; !q.empty() ; q.pop()) {
pos = q.front();
for (int i = fir[pos] ; i ; i = con[i].la) {
if (con[i].cap && (!dis[con[i].b])) {
dis[con[i].b] = dis[pos] + 1;
q.push(con[i].b);
}
}
}
if (!dis[en]) return 0;
return 1;
}
const int MAX = 10000010;
int isp[MAX + 10], pri[MAX / 10], pcnt, num[2];
set<int> prime;
void prework() {
for (int i = 2 ; i <= MAX ; ++ i) {
if (!isp[i]) pri[++pcnt] = i;
for (int j = 1 ; j <= pcnt && pri[j] * i <= MAX ; ++ j) {
isp[pri[j] * i] = 1;
if (i % pri[j] == 0) break;
}
}
for (int i = 2 ; i <= pcnt ; ++ i)
prime.insert(pri[i]);
}
int main() {
prework();
scanf("%d",&n);
for (int i = 1 ; i <= n ; ++ i) {
scanf("%d",&x[i]);
if (x[i] == 1 || x[i] != x[i-1] + 1)
p[++cnt] = x[i];
if (i > 1 && x[i] != x[i-1] + 1)
p[++cnt] = x[i-1] + 1;
}
p[++cnt] = x[n] + 1;
n = cnt;
st = ++n;
en = ++n;
for (int i = 1 ; i <= cnt ; ++ i) {
++ num[p[i]&1];
if (p[i]&1) {
add(st,i,1);
for (int j = 1 ; j <= cnt ; ++ j) {
int d = abs(p[i] - p[j]);
if (prime.count(d))
add(i,j,1);
}
} else add(i,en,1);
}
int ans = 0;
while (bfs())
ans += dfs(st,INF);
printf("%d
",ans + 2 * ((num[0] - ans) / 2 + (num[1] - ans) / 2) + ((num[1] - ans)&1) * 3);
return 0;
}
灏忕粨锛氬張鐪嬩簡涓€閬撴€濈淮棰樼殑棰樿В鈥︹€︽劅瑙夎〃绠楅珮濡欑殑鍚屾椂锛岃嚜宸辫繕娣辨湁涓嶈冻銆?/p>
以上是关于銆愬仛棰樸€慳rc080_f-Prime Flip鈥斺€旇浆鎹€佹暟璁哄強鍖归厤的主要内容,如果未能解决你的问题,请参考以下文章