• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 12,  comments - 40,  trackbacks - 0

            http://acm.pku.edu.cn/JudgeOnline/problem?id=3139
            Balancing the Scale
            去年上海賽區的B題,比賽沒有做出來

            注意隱含條件:每個數字不大于1024
            1、有這個條件后,則表達式之和不超過1024 * 10,這樣就可以開一個10240的表,記錄得到當前值的所有4字組合(用16位2進制表示)

            2、對于10240內的每兩個值進行配對(只要兩個4字組合中8個數字沒有重復的數字即可),每配一個對,則說明這個8字組合的分解方法數目加1(這個方法數目記錄在一個數組中,也用16位2進制表示8字組合)
            3、對于每兩個互補的8字組合,他們的分解方法數目相乘然后求和即是答案
            posted on 2007-08-09 22:34 LSM 閱讀(936) 評論(1)  編輯 收藏 引用 所屬分類: 其他

            FeedBack:
            # re: Poj 3139-Balancing the Scale[未登錄]
            2008-12-21 08:41 | feng
            #include <iostream>
            #include <algorithm>
            #include <cstring>

            using namespace std;

            const int MAX_VAL = 1024 * 4 + 1023 * 3 + 1022 * 2 + 1021;
            const int UNI_16 = 0xFFFF;

            struct Node {
            int comb;
            int cnt;
            Node* next;

            Node(int c, Node* n) {
            comb = c;
            cnt = 1;
            next = n;
            }
            };

            Node* g_value[MAX_VAL + 1];
            int g_num[16];
            int g_cnt8[UNI_16];

            void addValue(int value, int comb);
            bool card4(int set, int* num);
            void clear();
            void getAllCnts8();
            void getAllValues4();

            void addValue(int value, int comb) {
            Node*& head = g_value[value];
            if (head != NULL && head->comb == comb) {
            head->cnt++;
            }
            else {
            Node* node = new Node(comb, head);
            head = node;
            }
            }

            //If the cardinality of the set is 4,
            //then put the 4 numbers in to array num and return true.
            bool card4(int set, int* num) {
            int card = 0;
            int mask = 1;
            for (int i = 0; i < 16; i++) {
            if (mask & set) {
            if (card == 4) {
            card = 0;
            break;
            }
            num[card] = g_num[i];
            card++;
            }
            mask <<= 1;
            }
            return card == 4;
            }

            void clear() {//Free all the linked lists.
            for (int i = 0; i <= MAX_VAL; i++) {
            if (g_value[i] != NULL) {
            Node* p = g_value[i];
            while (p != NULL) {
            Node* pre = p;
            p = p->next;
            delete pre;
            }
            g_value[i] = NULL;
            }
            }
            }

            void getAllCnts8() {
            memset(g_cnt8, 0, sizeof(g_cnt8));
            for (int val = 0; val <= MAX_VAL; val++) {
            if (g_value[val] != NULL) {
            for (Node* i = g_value[val]; i != NULL; i = i->next) {
            for (Node* j = i->next; j != NULL; j = j->next) {
            if ((i->comb & j->comb) == 0) {
            g_cnt8[i->comb | j->comb] += i->cnt * j->cnt;
            }
            }
            }
            }
            }
            }

            void getAllValues4() {
            sort(g_num, g_num + 16);//Must sort, or "next_permutation()" won't work.
            int num[4];
            for (int comb = 0xF; comb <= 0xF000; comb++) {
            if (card4(comb, num)) {
            do {
            int value = num[0] * 4 + num[1] * 3 + num[2] * 2 + num[3];
            addValue(value, comb);
            } while (next_permutation(num, num + 4));
            }
            }
            }

            bool input() {
            bool hasNext = false;
            cin >> g_num[0];
            if (g_num[0] != 0) {
            hasNext = true;
            for (int i = 1; i < 16; i++) {
            cin >> g_num[i];
            }
            }
            return hasNext;
            }

            int solve() {
            getAllValues4();
            getAllCnts8();
            int cnt = 0;
            for (int comb8 = 0xFF; comb8 <= 0xFF00; comb8++) {
            cnt += g_cnt8[comb8] * g_cnt8[comb8 ^ UNI_16];
            }
            return cnt / 2;
            }

            int main() {
            memset(g_value, 0, sizeof(g_value));
            int i = 1;
            while (input()) {
            cout << "Case " << i << ": " << solve() << endl;
            clear();
            i++;
            }
            return 0;
            }

              回復  更多評論
              
            <2007年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            牛牛 ACM/ICPC

            最新隨筆

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            久久久精品久久久久久| 免费久久人人爽人人爽av| 99久久婷婷国产综合亚洲| 精品午夜久久福利大片| 久久精品不卡| 777米奇久久最新地址| 精品国产乱码久久久久久浪潮| 国产欧美久久久精品影院| 99久久久精品免费观看国产| 伊人久久一区二区三区无码| 久久99精品综合国产首页| 久久精品国产亚洲AV香蕉| 久久99久久99小草精品免视看| 国产毛片欧美毛片久久久| 久久国产免费| 久久国产精品偷99| 天天爽天天爽天天片a久久网| 一本久久免费视频| 久久久久综合国产欧美一区二区| 国产产无码乱码精品久久鸭| 色8久久人人97超碰香蕉987| 亚洲欧美一级久久精品| 天天影视色香欲综合久久| 国产激情久久久久影院小草 | 久久精品视频网| 国产成人久久精品一区二区三区| 无码人妻少妇久久中文字幕| 久久久黄片| 亚洲国产成人久久精品99| 久久久WWW成人| 欧美久久久久久精选9999| 欧美精品丝袜久久久中文字幕 | 久久国产精品视频| 久久综合成人网| 人人狠狠综合久久亚洲88| 久久综合欧美成人| 青草影院天堂男人久久| 午夜不卡888久久| 久久久久这里只有精品| 亚洲国产成人久久综合碰| 欧美一区二区久久精品|