• <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>

            C小加

            厚德 博學(xué) 求真 至善 The bright moon and breeze
            posts - 145, comments - 195, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            NYOJ 136 等式 解題報(bào)告

            Posted on 2012-01-30 01:40 C小加 閱讀(1604) 評論(0)  編輯 收藏 引用 所屬分類: 解題報(bào)告
            哈希。把a1*x13+a2*x23的所有情況存儲(chǔ)在哈希表中,然后用a3*x33+a4*x43+a5*x53去表中查找和為0的情況。用數(shù)組暴力的話會(huì)超內(nèi)存。
            這個(gè)題很久以前做過,雖然AC了但是錯(cuò)的。之前做的時(shí)候覺得有一組數(shù)據(jù)超int了(老是RE),而且當(dāng)時(shí)認(rèn)為只有這一組數(shù)據(jù)超了,所以當(dāng)時(shí)就把這組數(shù)據(jù)進(jìn)行特殊化處理了。后來小牛找出了問題我才意識到根本沒有超出int,我去掉特殊化處理后提交TLE,不是RE,我開始糾結(jié)當(dāng)時(shí)到底是怎么做的。
            我重新檢查了一遍代碼,發(fā)現(xiàn)我的哈希算法加上那組特殊數(shù)據(jù),時(shí)間復(fù)雜度O(n*m),n達(dá)到100萬,m達(dá)到1萬。這時(shí)間傷不起啊。后來想想哈希還可以優(yōu)化,于是我就改了一下哈希算法,復(fù)雜度降低到了O(n),最終也順利AC。
             
            #include <cstdio>
            #include <cmath>
            #include <cstdlib>
            #include <cstring>
            const int MAX=100003;
            const int MAXSUM=12500000;
            int a[1003];

            void g()
            {
                for(int i=-50;i<=50;i++)
                {
                    a[i+50]=i*i*i;
                }
            }


            template <class T>
            class hash
            {
            private:

                int pos;
                int next[MAX];
                int head[MAX];
                int key[MAX];
                int cnt[MAX];
            public:
                int count;
                void search(const int x);
                bool search1(const int x);
                void push(const int x);
                void clear();

            };

            template <class T>
            inline bool hash<T>::search1(const int x)
            {
                int temp=abs(x)%MAX;
                int t=head[temp];
                while(t!=-1)
                {
                    if (x==key[t])
                    {
                        cnt[t]++;
                        return true;
                    }
                    t=next[t];
                }
                return false;
            }

            template <class T>
            inline void hash<T>::search(const int x)
            {
                int temp=abs(x)%MAX;
                int t=head[temp];
                while(t!=-1)
                {
                    if (x==-key[t])
                    {
                        count+=cnt[t];
                    }
                    t=next[t];
                }
            }
            template <class T>
            inline void hash<T>::push(const int x)
            {
                if(search1(x)) return;
                int temp=abs(x)%MAX;

                if (head[temp]!=-1)
                {
                    next[pos]=head[temp];
                }
                head[temp]=pos;
                key[pos]=x;
                cnt[pos]=1;
                pos++;
            }
            template <class T>
            void hash<T>::clear()
            {
                count=0;
                pos=0;
                memset(next,-1,sizeof(next));
                memset(head,-1,sizeof(head));
                memset(cnt,0,sizeof(cnt));
            }
            hash<int> h;

            int main()
            {
                //freopen("in.txt","r",stdin);
                int T;
                scanf("%d",&T);
                memset(a,0,sizeof(0));
                g();
                while(T--)
                {

                    h.clear();
                    int a1,a2,a3,a4,a5;
                    int i,j,k;
                    int n;
                    scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
                    for(i=-50;i<=50;i++)
                    {

                        for(j=-50;i!=0&&j<=50;j++)
                        {
                            if(j==0) continue;
                            n=a1*a[i+50]+a2*a[j+50];
                            h.push(n);

                        }
                    }
                    for(i=-50;i<=50;i++)
                    {

                        for(j=-50;i!=0&&j<=50;j++)
                        {
                            for(k=-50;j!=0&&k<=50;k++)
                            {
                                if(k==0) continue;
                                n=a3*a[i+50]+a4*a[j+50]+a5*a[k+50];
                                if(n > MAXSUM || n < -MAXSUM)
                                    continue;
                                h.search(n);
                            }
                        }
                    }

                    printf("%d\n",h.count);
                }

                return 0;
            }
                    
            亚洲人成无码久久电影网站| 国产精品久久久久天天影视| 伊人伊成久久人综合网777| 免费精品国产日韩热久久| 久久久久久亚洲Av无码精品专口| 国产日产久久高清欧美一区| 欧美激情精品久久久久久| 77777亚洲午夜久久多喷| 99久久精品免费国产大片| 青青草原综合久久大伊人导航| 国产午夜免费高清久久影院| 欧美精品丝袜久久久中文字幕 | 国内精品伊人久久久久| 一本大道久久东京热无码AV| 日本免费久久久久久久网站| 久久综合亚洲鲁鲁五月天| 国产精品久久久久久久久鸭| 久久无码中文字幕东京热| 久久AⅤ人妻少妇嫩草影院| 国产成人无码久久久精品一| 97香蕉久久夜色精品国产| 久久精品成人免费观看97| 色综合久久88色综合天天| 久久久久久久97| 亚洲精品无码久久久影院相关影片 | 91久久精品91久久性色| 亚洲精品乱码久久久久久久久久久久 | 国产精品久久久久影院色| 三级三级久久三级久久| 亚洲国产天堂久久综合| 欧美精品丝袜久久久中文字幕| 久久99精品九九九久久婷婷| 激情综合色综合久久综合| 办公室久久精品| 久久99精品免费一区二区| 国产日韩久久久精品影院首页| 99热精品久久只有精品| 久久久久久国产精品美女| 亚洲&#228;v永久无码精品天堂久久 | 韩国免费A级毛片久久| 国产人久久人人人人爽|