• <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 - 195,  comments - 30,  trackbacks - 0

            Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions:

            17 + 5 + -21 + 15 =  16
            17 + 5 + -21 - 15 = -14
            17 + 5 - -21 + 15 =  58
            17 + 5 - -21 - 15 =  28
            17 - 5 + -21 + 15 =   6
            17 - 5 + -21 - 15 = -24
            17 - 5 - -21 + 15 =  48
            17 - 5 - -21 - 15 =  18
            

            We call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.

            You are to write a program that will determine divisibility of sequence of integers.

            Input

            There are multiple test cases, the first line is the number of test cases.
            The first line of each test case contains two integers, N and K (1 ≤ N ≤ 10000, 2 ≤ K ≤ 100) separated by a space.

            The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.

            Output

            Write to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it's not.

            Sample Input

            2
            4 7
            17 5 -21 15
            4 5
            17 5 -21 15
            

            Sample Output

            Divisible
            Not divisible
            
            啟發(fā):1,涉及整除就要聯(lián)想取模!!?。。。。?!
                        2,這是有層次性,不要搞混,以下是錯(cuò)誤代碼 
            #include<iostream>
            #include
            <cstdlib>
            using namespace std;
              
              
            int main()
              
            {
              freopen(
            "s.txt","r",stdin);
              freopen(
            "key.txt","w",stdout);
              
            int testnum;
              
            int num,pos;
              cin
            >>testnum;
              
            int a[101],temp,i,j;
              
            while(testnum--)
              
            {
                    memset(a,
            0,sizeof(a));
                    cin
            >>num>>pos;
                    cin
            >>temp;
                    temp
            %=pos;
                    
            if(temp<0)temp+=pos;
                    a[temp]
            =1;    
                    
            for(i=1;i<num;i++)
                    
            {
                        cin
            >>temp;
                        temp
            %=pos;
                        
            if(temp<0)temp+=pos;//temp%pos¿ÉÄÜÊǸºÊý 
                        for(j=0;j<pos;j++)
                        
            {
                            
            if(a[j]>0)
                              
            {
                                    a[(j
            +temp)%pos]++;
                                    
            if((j-temp)<0)
                                    
            {
                                        a[(j
            -temp)+pos]++;
                                    }

                                    
            else
                                    
            {
                                    a[j
            -temp]++;
                                    }

                                    
            if(temp!=0)
                                    a[j]
            =0;
                              }

                        }

                    }

                    
            if(a[0]>0)cout<<"Divisible"<<endl;
                    
            else cout<<"Not divisible"<<endl;
                }


              
            //system("PAUSE");
              return   0;
              }

            這是有層次性,再添加一個(gè)元素時(shí),只能改變前一組a[j]的值

             if(a[j]>0)//
                  {
                  a[(j+temp)%pos]++;//這里的修改應(yīng)該不應(yīng)添加到前面的a[j]中去。
                        if((j-temp)<0)

            所以應(yīng)當(dāng)用兩個(gè)數(shù)組。

            #include<iostream>
            #include
            <cstdlib>
            using namespace std;
              
              
            int main()
              {
              freopen(
            "s.txt","r",stdin);
              freopen(
            "key.txt","w",stdout);
              
            int testnum;
              
            int num,pos;
              cin
            >>testnum;
              
            int a[101],temp,i,j;
              
            int b[101];
              
            while(testnum--)
              {
                    memset(a,
            0,sizeof(a));
                    memset(a,
            0,sizeof(b));
                    cin
            >>num>>pos;
                    cin
            >>temp;
                    temp
            %=pos;
                    
            if(temp<0)temp+=pos;
                    a[temp]
            =1;    
                    
            for(i=1;i<num;i++)
                    {
                        cin
            >>temp;
                        temp
            %=pos;
                        
            if(temp<0)temp+=pos;//temp%pos¿ÉÄÜÊǸºÊý 
                        for(j=0;j<pos;j++)
                        {
                            
            if(a[j]>0)
                              {
                                    b[(j
            +temp)%pos]++;
                                    
            if((j-temp)<0)
                                    {
                                        b[(j
            -temp)+pos]++;
                                    }
                                    
            else
                                    {
                                    b[j
            -temp]++;
                                    }
                              }
                        }      
                              memset(a,
            0,sizeof(a));
                              
            for(j=0;j<pos;j++)
                              {
                                    
            if(b[j]>0)
                                    a[j]
            =1;
                              }
                              memset(b,
            0,sizeof(b));
                    }
                    
            if(a[0]>0)cout<<"Divisible"<<endl;
                    
            else cout<<"Not divisible"<<endl;
                }

              
            //system("PAUSE");
              return   0;
              }
            posted on 2009-06-30 22:20 luis 閱讀(258) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2012年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            友情鏈接

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久久精品波多野结衣| 国产激情久久久久久熟女老人 | 久久综合久久伊人| 久久精品视频一| 久久国产精品久久国产精品| 久久久久亚洲AV综合波多野结衣| 伊人久久大香线蕉综合Av| 国产精品欧美久久久天天影视| 国产精品99久久久久久宅男| 综合久久国产九一剧情麻豆| 丰满少妇人妻久久久久久4| 久久久亚洲裙底偷窥综合| 国产精品va久久久久久久| 久久精品国产精品亚洲精品| 国产福利电影一区二区三区久久久久成人精品综合 | 久久精品国产亚洲AV不卡| 色综合久久88色综合天天| 99久久精品国产一区二区| 99久久婷婷国产一区二区| 欧美大香线蕉线伊人久久| 日本国产精品久久| 国产成人综合久久久久久| 久久精品国产亚洲av麻豆图片| 久久人人爽人人澡人人高潮AV| 精品亚洲综合久久中文字幕| 色88久久久久高潮综合影院| 欧美激情精品久久久久久久| 久久精品18| 久久免费国产精品一区二区| 久久99国产精品尤物| 中文精品久久久久人妻不卡| 久久婷婷是五月综合色狠狠| 久久精品无码专区免费| 久久免费小视频| 国产成人无码精品久久久久免费| 精品久久久久久中文字幕人妻最新| 色欲久久久天天天综合网精品| 久久精品国产亚洲AV香蕉| 少妇无套内谢久久久久| 性高湖久久久久久久久AAAAA| 久久久久亚洲AV成人网|