Question 1380: lucky number, 1380 lucky

Source: Internet
Author: User

Question 1380: lucky number, 1380 lucky

Reprinted please indicate text link http://blog.csdn.net/yangnanhai93/article/details/40441709


Question link address: http://ac.jobdu.com/problem.php? Pid = 1, 1380

The first time I officially wrote this ACM algorithm blog, I felt that this question was a little touched. Many times more algorithms need to be observed and I need to carefully taste the question, to be able to: "Aha, algorithm"-"programming Pearl River"

Let's talk about our inertial thinking first, because this question is obviously a statistical problem, so I am trying to reduce the memory overhead as much as possible, so I am using:

In my opinion, this should be okay, because int 4 bytes char 1 byte, because m % n! = 0, and n> = 2, so the number of elements in this map is less than or equal to 5*5*10 ^ 5 = Mb, And then I reduced the code, we hope to get a lower overhead, but we find that the overhead of using int short as map is the same, which cannot be understood.

#include <stdio.h>#include <map>using namespace std; int main(){    map<int,char> mp;    map<int,char>::iterator key;    int n,m,num;    while(scanf("%d%d",&n,&m)!= EOF)    {        mp.clear();        while(m--)        {            scanf("%d",&num);            key = mp.find(num);            if(key == mp.end())                mp.insert(pair<int,char>(num,'1'));                    else if(key->second<(n+'0'))                key->second++;           }        key=mp.begin();        while(key!=mp.end()&&key->second==(n+'0'))            key++;        printf("%d\n",key->first);    }    return 0;}/**************************************************************    Problem: 1380    User: vincent_ynh    Language: C++    Result: Memory Limit Exceed****************************************************************/

The second idea that can reduce the space is to find that only four-bit storage is needed, and then store it with bits. Here, because of my personal misunderstanding, mistaken for the number 1-10 ^ 6, and then wrote a code with a bit of storage, rarely write code with a bit of operation, it is ugly to write, however, I think that coder, as a coder to keep improving, still needs to be used more. The example of the first phone book in programming Pearl River, touched me a lot, it makes me feel that the code can be so clever. Well, it's far away. Here I will post it. If the data range is small, you can also use the following bit of storage code:

#include <stdio.h>#include <memory.h>int main(){    int A[300000],B[2]={15,240};    int n,m,num;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(A,0,sizeof(A));        while(m--)        {            scanf("%d",&num);            int tmp=(num-1)/2;            int x=(num-1)%2;            if((A[tmp]&B[x])>>(4*x)<=n)            {                A[tmp]=A[tmp]+(1<<(4*x));            }        }        for(int i=1;;i++)        {            int tmp=(i-1)/2;            int x=(i-1)%2;            if(((A[tmp]&B[x])>>(4*x))%n!=0)            {                printf("%d\n",i);                break;            }        }    }    return 0;}/**************************************************************    Problem: 1380    User: vincent_ynh    Language: C++    Result: Runtime Error****************************************************************/
Finally, I can think of the current Optimal Algorithm for this question:

We perform binary encoding on each number to obtain a 32-bit number. If this number appears n times, this encoding should appear n times, if n is obtained, the number of n occurrences is eliminated. I figured this out, and then there were no n times, so m % n times, of course, m % n + x * n, which would not be affected, in the remaining array, the lucky number encoding is left (each bit is multiplied by m % n). Therefore, we only need to take the remainder (% n) of each bit in the array) at the same time, change non-0 bits to 1, and the BITs remain unchanged. Finally, convert the array to an integer.

The main idea of the code can be found through examples:

2 5

1 1 2 2 3

Result 3

The code is 00000001 00000001 00000010 00000010 00000011 (because the number is small, the first 3 digits are omitted, that is, 24 0)

After the data is integrated into the array, the remainder is 00000033 and then 00000011.

Then convert a non-zero value to 1 and get 00000011

Converts an array to an integer of 3, so the result is 3.


Here we will add the bitwise AND (&) operation. bitwise AND (&) operations are binary encoding of numbers. If the bitwise is the same as 1, it is 1; otherwise, it is 0.


The actual code is as follows:

#include <stdio.h>#include <memory.h> int main(){    int A[32],m,n,num;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(A,0,sizeof(A));        for(int j=0;j<m;j++)        {            scanf("%d",&num);            for(int i=0;i<32;i++)            {                A[i]=A[i]+(num&1);                A[i]=A[i]%n;                num=num>>1;            }        }        int tmp=m%n,result=0;        for(int i=0;i<32;i++)        {            if(A[i]%n==tmp)                result+=1<<i;         }        printf("%d\n",result);    }    return 0;}/**************************************************************    Problem: 1380    User: vincent_ynh    Language: C++    Result: Accepted    Time:1620 ms    Memory:1020 kb****************************************************************/


Anyone who wants to learn algorithms will be welcomed: vincent_ynh@163.com



Youe lucky number: & amp; lt; A + B + C + D & amp; gt; ÷ E?

Lucky number brings us good luck? Do not give points if you write it. Tired. We are witnessing more frequent usage of so-called lucky numbers which contain special

Lucky Number lyrics

Song name: Lucky Number
Singer: Tsai Yilin
Album: Lucky Number

Lyrics: Xie mengjuan compose: Li weiyun: Martin Tang
OS: hey, you know something?
I dreamed of you last night ..
And, Jenet told me,
You gotta be mine
Baby my lucky number is not 5
Baby my lucky number is not 9
Oh dream is half done
Heartbeat: One hundred
Baby my favorite color is not white
Baby my favorite color is not black
Oh, the dream will be wonderful.
Have your color
Heard that before
You are the nightmare of girls
After hearing
Soon after
We will be in love
No
Alas, love is wonderful.
Expand my love
Love is a glorious thing
Love is coming back again and again
Love is believing in yourself
Because I love you.
Do not repent
Love said exactly at midnight
As long

Music.baidu.com/song/816414

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.