国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

codeforcesRound#259(div2)D解題報告

來源:懂視網 責編:小采 時間:2020-11-09 08:02:31
文檔

codeforcesRound#259(div2)D解題報告

codeforcesRound#259(div2)D解題報告:D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements
推薦度:
導讀codeforcesRound#259(div2)D解題報告:D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements

解法:

這里題目給了幾個很顯眼的條件,ai限制在了1~30之間,由于可以bi無限選1這個數,那么|ai-bi| 最大就是29了,意味著bi < 59的。

要求所有的bi互質,可以化為所有的bi分解出來的質因數均不相同,bi < 59,有16個質數。這里我們很容易聯想到狀態壓縮DP了。

用s表示當前階段用了哪些質因數的狀態,例如 s = 3 = 11 代表目前狀態下使用了第一個和第二個質因數。

很快我們就可以寫出狀態轉移方程:

f[i][s] = min(f[i-1][s^c[k]] + abs(a[i] - k))。 其中c[k]表示數字k使用了哪些質因數。

代碼:

#include 
#include 
#include 
#define M_max 60
#define N_max 123
#define inf 0x3f3f3f3f

using namespace std;

int p[N_max], c[M_max], a[N_max];
int f[N_max][1<<16], pre[N_max][1<<16][2];
int n, cnt, minnum, minpos;


void prime() {
	for (int i = 2; i <= M_max; i++) {
	bool flag = false;

	for (int j = 2; j <= sqrt(i); j++)
	if (i%j == 0) {
	flag = true;
	break;
	}

	if (!flag) p[++cnt] = i;
	}

	for (int i = 1; i <= M_max; i++)
	for (int j = 1; j <= cnt; j++)
	if (i%p[j] == 0)
	c[i] |= 1 << (j-1);
}

void init() {
	prime();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
}

void print(int x, int pos) {
	if (x == 0) return;
	print(x-1, pre[x][pos][0]);
	printf("%d ", pre[x][pos][1]);
}

void solve() {
	memset(f, inf, sizeof(f));
	memset(f[0], 0, sizeof(f[0]));
	minnum = inf;

	for (int i = 1; i <= n; i++)
	for (int s = 0; s < (1<<16); s++)
	for (int k = 1; k <= M_max; k++)
	if ((s&c[k]) == c[k]) {
	int tmp = f[i-1][s^c[k]] + abs(a[i]-k);

	if (tmp < f[i][s]) {
	f[i][s] = tmp;
	pre[i][s][0] = s^c[k];
	pre[i][s][1] = k;
	}
	}
	for (int s = 0; s < (1<<16); s++)
	if (f[n][s] < minnum) {
	minnum = f[n][s];
	minpos = s;
	}

	print(n, minpos);
}

int main() {
	init();
	solve();
}

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

codeforcesRound#259(div2)D解題報告

codeforcesRound#259(div2)D解題報告:D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements
推薦度:
標簽: 報告 解題 round
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 久久精品一区二区三区不卡牛牛 | 一级特黄牲大片免费视频 | 久久精品免费观看 | 日韩欧美高清视频 | 欧美国产日韩在线观看 | 亚洲人成网站999久久久综合 | 欧美日韩国产一区 | 另类区 | 国内精品线在线观看 | 夜色毛片永久免费 | 国产成人精品在视频 | 与子乱刺激对白在线播放 | 97成人免费视频 | 亚欧成人一区二区 | 午夜视频在线 | 国内精品视频在线播放 | 日韩 亚洲 制服 欧美 综合 | 欧美精品免费在线观看 | 日韩欧美高清一区 | 亚洲欧美中文日韩综合 | 国产精品v欧美精品v日本精 | 久久精品无遮挡一级毛片 | 亚洲国产成人久久综合碰碰动漫3d | 中国一级全黄的免费观看 | 激情一区| 久久综合爱 | 亚洲国产精品婷婷久久久久 | 婷婷国产 | 国产国拍亚洲精品永久不卡 | zozozo欧美人禽交另类视频 | 亚洲免费久久 | 成人精品视频在线观看 | 在线观看欧美日韩 | 欧美日韩91| 国产午夜在线观看 | 欧美成人精品一级高清片 | 夜夜操夜夜爱 | 亚洲一区自拍 | 欧美日韩a | 在线观看亚洲一区二区 | 在线欧美日韩制服国产 |