POJ 1977 constructor Matrix Multiplication

Source: Internet
Author: User

Question:

There are N people with Ai tags in their hands. This person can add tags to the specified K people (his/her own tags will not be less ).

A person with an odd number of tags has the right to vote. The number of tickets in the last ticket is an odd number.

Train of Thought. Take the input example as an example:

Initial status matrix: A = | 210 |

The status transition matrix is:

| 1, 111 |

B = | 001 |

| 1, 100 |

The status after the first round ends: (A * B +)

The status after the second round is: (A * B + A) * B +(A * B + A) => A * (B + E) * B + A * (B + E) => A * (B + E) ^ 2;

After the third round, A * (B + E) ^ 3

This is the rule;

In addition, it should be noted that when the number is an odd number, an even number does not have the right to vote.

Let's take a look at the matrix below.

| AB | * | EF | = | A * E + B * g a * F + B * H |

| GH |

What exactly does it mean?

We can see this:

1. There is A voting opportunity. Each chance is to vote for 1E and 2 GB respectively.

2. There are B voting opportunities. Each chance is to vote for 1F and 2 h respectively.

In this way, the ticket for 1 is A * E + B * G.

It can be found that when ABEFGH is an even number, it is useless.

Therefore, % 2 is correct. Furthermore, the Union Law of the matrix is used. After the analysis is complete, click "AC.

I found that my code can be rank10 ~ Haha

#include<iostream>#include<string.h>#include<map>#define MAXN 222using namespace std;typedef short ll;struct node{    ll ma[MAXN][MAXN];    void init(){   memset(ma,0,sizeof(ma));   }}res,temp,L;int allocp;void init(){  allocp=0;  res.init();  temp.init();  L.init();  for( int i=0;i<MAXN;i++ )    res.ma[i][i]=1;}void set_Matrix(){  for( int i=0;i<allocp;i++ )    temp.ma[i][i]++;}node matriXmult( node a,node b ){  node c;  memset( c.ma,0,sizeof(c.ma) );  for( int i=0;i<allocp;i++ )  for( int k=0;k<allocp;k++ )  if( a.ma[i][k] )  for( int j=0;j<allocp;j++ )    c.ma[i][j]+=a.ma[i][k]*b.ma[k][j];  for( int i=0;i<allocp;i++ )  for( int j=0;j<allocp;j++ )    c.ma[i][j]%=2;  return c;}void matrix_Power( int t ){  for( int i=0;i<31;i++ )  {    if( t&(1<<i) )      res=matriXmult(res,temp);    temp=matriXmult(temp,temp); }}int main(){ int T,n,t,mark,list; string str1,str2; cin>>T; while( T-- ) {    init();    map<string,int>map;    map.clear();    cin>>n>>t;    while( n-- )    {      cin>>str1;      if( map.find(str1)==map.end() )        map[str1]=allocp++;      cin>>mark>>list;      L.ma[0][map[str1]]=mark;      for( int i=0;i<list;i++ )      {         cin>>str2;         if( map.find(str2)==map.end() )            map[str2]=allocp++;         temp.ma[map[str1]][map[str2]]++;     }      }      t--;      set_Matrix();      matrix_Power(t);      node kk;      kk.init();      for( int i=0;i<1;i++ )      for( int j=0;j<allocp;j++ )      for( int k=0;k<allocp;k++ )      kk.ma[i][j]+=L.ma[i][k]*res.ma[k][j];      int sum=0;      for( int i=0;i<allocp;i++ )      sum+=( kk.ma[0][i]%2 );      cout<<sum<<endl;  } return 0;}



A has A vote for EE and GG respectively.

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.