0. ã¯ããã«
bit å ¨æ¢ç´¢ã¯ãä¸ã®ä¸ã§ãAtCoder 温室è²ã¡ã ã¨å¼±ããã¨è¨ãããã¿ã¤ãã®åé¡ã®ä»£è¡¨ããããã¾ãããä½ãèããã«æèåæ¢ãã¦å ¨æ¢ç´¢ããã°ããã®ã§ãããã¡ãã£ã¨å®è£ ãéããå¾åã«ãã£ã¦ãæ¸ãåãã®ã大å¤ã¨ããæãã§ããdifficulty ãè¦ã¦ã ABC-C ã®ä¸ã§ãé«ãã®å¾åã§ãã
- ABC 147 C - HonestOrUnkind2 (972)
- ABC 128 C - Switches (728)
- ABC 119 C - Synthetic Kadomatsu (1306)
- ABC 104 C - All Green (1396)
bit å ¨æ¢ç´¢ã®èãæ¹ãå®è£ ã¯ã以ä¸ã®è¨äºã«ã¾ã¨ãã¦ã¿ã¾ããã
ããããå ¨æ¢ç´¢ã¢ã«ã´ãªãºã ã«ããã¦ãããæ±ç¨çãªæ¹æ³ã¨ãã¦å帰é¢æ°ãç¨ããã¨ããã®ãããã¾ããããããå帰é¢æ°ãèªå¨ã«æ¸ãããªãããªãã°ãbit å ¨æ¢ç´¢ãè¦ããå¿ è¦ã¯ãªããããããªãã§ãã
ããã§ä»åã¯ãbit å ¨æ¢ç´¢ã§å®çªã®ãé¨åååé¡ããå帰é¢æ°ã§è§£ãã¦ã¿ã¾ããä¸å£ã«ãå帰ã§è§£ããã¨ãã£ã¦ãæå¤ã¨æ¸ãæ¹ãããããããã¾ããç´°ããéããç¡è¦ããã¨ããã£ãã以ä¸ã® 2 種é¡ã§ãããã...
- æ å ±ãé ã£ã¦ããæ¸ãæ¹
- æ å ±ãéãã¦ããæ¸ãæ¹
ãããã®éãã¯ãããã»ã©æ¬è³ªã§ã¯ãªãããããã¾ããããå¼·ãã¦è¨ãã°å¾è ã®æ¹ãã¡ã¢åã«ãã£ã¦ DP ã«ããããããªã¨æãã¾ãã
ã
1. é¨åååé¡
bit å ¨æ¢ç´¢ã®ä¾ã¨ãã¦ãã¾ãã«ãæåãªåé¡ã§ããDP ã®ç·´ç¿ã§ãåºãåé¡ã§ããã
åºæ¬çã«ã¯ãåæ´æ°ã«ã¤ãã¦ãé¸ã¶ããé¸ã°ãªããã¨ãã 2 éããã¤ã®é¸æè¢ãããã®ã§ã調ã¹ãã¹ãå ´åã®æ°ã¯
éã
ã¨ãããã¨ã«ãªãã¾ãã
åé¡æ¦è¦
åã®æ£ã®æ´æ° ã¨ãæ£ã®æ´æ° ã¨ãä¸ãããã¾ãã
ã®ä¸ããä½åãé¸ãã§ç·åã ã¨ãããã¨ãã§ãããã©ãããå¤å®ãã¦ãã ããã
å¶ç´
ã
2. æ å ±ãé ã£ã¦ããæ¸ãæ¹
ã²ãã£ã¨ããããé¨åååé¡ãå帰é¢æ°ã使ã£ã¦è§£ããã¨è¨ãããããå¤ãã®æ¹ããã®æ¸ãæ¹ãããããããã¾ããã
æåéãã éãã®åè¨å¤ããã¹ã¦ç®åºãããã®ä¸ã« ãå«ã¾ãããã©ããããã§ãã¯ããèãæ¹ã§ãããã¨ãã°
ã®å ´åãä¸å³ã®ãããªæãã§ãã
ãã®æ§åãå帰é¢æ°ãç¨ãã¦å®è£ ããã¨ã次ã®ããã«æ¸ããã§ããããsum ã¨ããå¤æ°ã«ãä»ã¾ã§ã®æ å ±ããè¾¼ãããã¦ãã¦ããããæªæ¥ã¸ã¨é ã£ã¦ããã¤ã¡ã¼ã¸ã§ããè¨ç®é㯠ã«ãªãã¾ãã
#include <iostream> #include <vector> using namespace std; // å ¥å int N, W; vector<int> a; bool rec(int i, int sum) { // æçµãã§ã㯠if (i == N) { if (sum == W) return true; else return false; } // é¸ã¶å ´åãåå² if (rec(i + 1, sum + a[i])) return true; // é¸ã°ãªãå ´åãåå² if (rec(i + 1, sum)) return true; // ã©ã¡ãããã¡ return false; } int main() { cin >> N >> W; a.resize(N); for (int i = 0; i < N; ++i) cin >> a[i]; // 解ã if (rec(0, 0)) cout << "Yes" << endl; else cout << "No" << endl; }
注æ
ãã®æ¸ãæ¹ã¯èªç¶ãªã®ã§ãããããå°ãè¤éãªåé¡ã«å¯¾ããè¤éãªå®è£ ã«ãªãã¨ãã¡ã¢åãã¥ãããªãå¾åã«ããæ°ããã¾ãããã®ãããã®äºæ ã¯ã診æ人ããããªã¢ãªãã£æº¢ããææ ã¨ã¨ãã«ç¶´ã£ã¦ãã¾ãã
ãã®ãé ãæ¸ãæ¹ãã¯ãåçè¨ç»æ³ããããæåãæ¢ç´¢ãããå ´åã«æå¹ãªã¤ã¡ã¼ã¸ãããã¾ãã
ã
3. æ å ±ãéãã¦ããæ¸ãæ¹
ããåçè¨ç»æ³ã«è¿ãèãæ¹ã«ãªãã¾ããå ã®åé¡ãé¨ååé¡ã«å解ãã¦ããããã®è§£ãç¨ãã¦å ã®åé¡ã«çããæãã¤ã¡ã¼ã¸ã§ãï¼ï¼ï¼
ã©ããã£ã¦åé¡ãåå²ãããããã§ãããããã³ãã¯ãå ´ååããæèãããã¨ãã§ããä»åã®é¨åååé¡ã§ã¯ã以ä¸ã® 2 ã¤ã®å ´åã«åãã¦èãã¦ã¿ã¾ãããã
- ãé¸ã¶å ´å
- ãé¸ã°ãªãå ´å
åè ã®å ´åãå®ã¯ãã åã®æ´æ° ã®ä¸ããããã¤ãé¸ãã§ç·åã ã«ãããã¨ã¯å¯è½ãï¼ãã¨ããé¨ååé¡ã«ãªãããã§ãããå¾è ã¯ãã åã®æ´æ° ã®ä¸ããããã¤ãé¸ãã§ç·åã ã«ãããã¨ã¯å¯è½ãï¼ãã¨ããé¨ååé¡ã«ãªãã¾ãã
ãããã¦ãå
ã®åé¡ã¯ 2 ã¤ã®é¨ååé¡ã«å解ããã¾ããï¼ï¼ï¼
å
ã®åé¡ã¯ åã®æ´æ°ã«é¢ããåé¡ã§ããããé¨ååé¡ã¯ãããã åã®æ´æ°ã«é¢ããåé¡ã§ãããããã®é¨ååé¡ãå帰é¢æ°ã«æ¼ãä»ãã¦ãå帰çã«è§£ãã¦ããããã§ããããã¦ãã®é¨ååé¡ã®çããã¾ã¨ãããã¦ãå
ã®åé¡ã®çããä½ãä¸ãã¦ããã¾ããä»åã§ããã°ã
- 2 ã¤ã®é¨åååé¡ã®ãã¡ãå°ãªãã¨ãä¸æ¹ã Yes ãªãã°ãå ã®åé¡ã®çãã Yes
- 2 ã¤ã®é¨åååé¡ã®ãã¡ãã©ã¡ãã®çãã No ãªãã°ãå ã®åé¡ã®çãã No
ã¨ãããã¨ã«ãªãã¾ãã
ãã¦ã åã®æ´æ°ã«é¢ããåé¡ã åã®æ´æ°ã«é¢ããé¨ååé¡ã¸ã¨å解ããã¾ãããããããä»åº¦ã¯ åã®æ´æ°ã«é¢ããé¨ååé¡ã¸ã¨å解ãã¦ããã¾ãã
ä¸è¬ã«ã åã®æ´æ°ã«é¢ããåé¡ãã åã®æ´æ°ã«é¢ãã 2 ã¤ã®åé¡ã«å解ãã¦ãã¾ã¨ããããããã«ãã¾ãããã¨ãã°
ã®å ´åãå ¨ä½ã®ã¤ã¡ã¼ã¸ã¯ä¸å³ã®ãããªæãã§ãã
ã¤ã¾ããã åã®æ´æ°ã«é¢ããåé¡ããå解ããã¦ãããã¡ã«å¾ã ã«å°ãããªã£ã¦ãæå¾ã¯ã åã®æ´æ°ã«é¢ããåé¡ãã«ãªãã¾ãã åã®æ´æ°ã®ç·å㯠ã«ãããªããªãã®ã§ã
- ãä½ãåé¡ãªã Yes
- 以å¤ãä½ãåé¡ãªã No
ã¨ãªãã¾ãããã®æ å ±ãä¸ã¸ä¸ã¸ã¨ä¼æãã¦ãããæçµçã«å ã®åé¡ã®çããæ±ã¾ãã¾ãã以ä¸ãå®è£ ããã¨ã次ã®ããã«æ¸ãã¾ããè¨ç®é㯠ã«ãªãã¾ãã
#include <iostream> #include <vector> using namespace std; // å ¥å int N, W; vector<int> a; // a[i], a[i+1], ..., a[N-1] ã®ç·åã w ã«ã§ãããï¼ bool rec(int i, int w) { // æçµãã§ã㯠if (i == N) { if (w == 0) return true; else return false; } // é¸ã¶å ´åãåå² if (rec(i + 1, w - a[i])) return true; // é¸ã°ãªãå ´åãåå² if (rec(i + 1, w)) return true; // ã©ã¡ãããã¡ return false; } int main() { cin >> N >> W; a.resize(N); for (int i = 0; i < N; ++i) cin >> a[i]; // 解ã if (rec(0, W)) cout << "Yes" << endl; else cout << "No" << endl; }
å¾ããããã
ç´°ããéããªã®ã§ãããä¸ã®å帰é¢æ°ã§ã¯ãé¨ååé¡ã®åãåºãæ¹ãã¨ã¯
- ã®ç·åã ã«ã§ããã
ã¨ãã風ãªæãã§ããããªãã¨ãªãä¸èªç¶ãªæ°ããã¾ãããããã次ã®ããã«ãããã¨ãå¤ãããã«æãã¾ãã
ãããèªç¶ãªé¨ååé¡ã
ã®ç·åã ã«ã§ããã
ãããã風ã«æ¸ãç´ãã¦ã¿ã¾ãããã
#include <iostream> #include <vector> using namespace std; // å ¥å int N, W; vector<int> a; // a[0], a[1], ..., a[i-1] ã®ç·åã w ã«ã§ãããï¼ bool rec(int i, int w) { // æçµãã§ã㯠if (i == 0) { if (w == 0) return true; else return false; } // é¸ã¶å ´åãåå² if (w >= a[i - 1] && rec(i - 1, w - a[i-1])) return true; // é¸ã°ãªãå ´åãåå² if (rec(i - 1, w)) return true; // ã©ã¡ãããã¡ return false; } int main() { cin >> N >> W; a.resize(N); for (int i = 0; i < N; ++i) cin >> a[i]; // 解ã if (rec(N, W)) cout << "Yes" << endl; else cout << "No" << endl; }
ã¡ã¢åãã㨠DP
ããã»ã©ã®å®è£ ã¯ãã®ã¾ã¾ã§ã¯ ã§ãããããã»ãã®ã¡ãã£ã¨ã®å·¥å¤«ã«ãã£ã¦ããªã㨠DP ã«ãªã£ã¦ãã¾ãã¾ãï¼ï¼ï¼å帰é¢æ°ã®å®ç¾©ã¯
rec(i, w) := ã®ç·åã ã«ã§ããã
ã§ããããã®çããã¡ã¢ããã®ã§ããããããã¨ä»¥ä¸ã®ãããªå®è£ ã«ãªãã¾ãããã®è¨ç®é㯠ã§ãï¼
#include <iostream> #include <vector> using namespace std; // å ¥å int N, W; vector<int> a; // a[0], a[1], ..., a[i-1] ã®ç·åã w ã«ã§ãããï¼ vector<vector<int>> dp; // -1: æªæ±ã0: Noã1; Yes int rec(int i, int w) { // ã¡ã¢ããã§ã㯠if (dp[i][w] != -1) return dp[i][w]; // æçµãã§ã㯠if (i == 0) { if (w == 0) return true; else return false; } // å帰çã«æ±ãã int res = 0; if (w >= a[i - 1] && rec(i - 1, w - a[i-1]) == 1) res = 1; if (rec(i - 1, w) == 1) res = 1; // ã¡ã¢ããªããè¿ã return dp[i][w] = res; } int main() { cin >> N >> W; a.resize(N); for (int i = 0; i < N; ++i) cin >> a[i]; // 解ã dp.assign(N+1, vector<int>(W+1, -1)); // -1 ã§åæå if (rec(N, W) == 1) cout << "Yes" << endl; else cout << "No" << endl; }
ã
4. ã¾ã¨ã
bit å ¨æ¢ç´¢ã¯ä¸ã¤ã®ç»ç«éã«ãªã£ã¦ããã¤ã¡ã¼ã¸ãããã¾ãããã¡ãããããåãèªå¨ã«ä½¿ãããªãããã¨ã¯ã¨ã¦ãéè¦ã§ããããããã以ä¸ã«ãæ±ç¨çãªå帰é¢æ°ãèªå¨ã«ä½¿ãããªãããã¨ãéè¦ããªã¨æãã¾ããå帰é¢æ°ãç¨ããæ¢ç´¢ãèªå¨ã«ä½¿ããã¨ãåçè¨ç»æ³ã«ãã¤ãªããã¾ãï¼ï¼ï¼