Baidu Star Astar ROUND2A

Source: Internet
Author: User

All X

Geometric series sum up A/b mod C = A mod (b*c)/B or divide it up A bit

Sitting in line

Pressure + topology DP

The DP (i, j) indicates that the current binary state is J, the last digit of the current state is a[i], and then is updated according to the topological order DP and is logged in the queue with a bool array.

There are other graceful postures on the web, and the topological order can be achieved by some enumeration method.

1#include <bits/stdc++.h>2typedefLong Longll;3 using namespacestd;4 inta[ -], p[ -], N;//whether the number of input i is fixed5 intvis[ -], now[ -];//whether the number of outputs is determined6ll dp[ -][1<< -];7 BOOLisin[ -][1<< -];8 structp{9     intx, y;Ten P () {} OnePintXinty): x (x), Y (y) {} A }; -  - intMain () { the     intT, CA =1; -Cin>>T; -      while(t--){ -memset (Vis,0,sizeof(Vis)); +Cin>>N; -          for(inti =0; I < n; i++){ +Cin>>a[i]>>P[i]; A             if(P[i]! =-1){ atvis[P[i]] =true; -now[P[i]] =i; -             } -         } -  -Memset (DP,0x80,sizeof(DP)); inmemset (Isin,0,sizeof(Isin)); -Queue<p>Q; to         if(vis[0]){ +dp[now[0] ][1<<now[0] ] =0; -isin[now[0] ][1<<now[0] ] =true; theQ.push (P (now[0],1<<now[0]) ); *         } $         Else{Panax Notoginseng              for(inti =0; I < n; i++) -                 if(P[i] = =-1){ thedp[i][1<<i] =0; +isin[i][1<<i] =true; AQ.push (P (i,1<<i)); the                 } +         } -  $          for(inti =1; I < n; i++) {//POS $             intSize =q.size (); -              for(intj =0; J < size; J + +){ -P pp =Q.front (); the Q.pop (); -                 if(Vis[i]) {Wuyidp[Now[i] [pp.y| (1<<now[i])] = max (dp[now[i] [pp.y| (1<<now[i]), dp[pp.x][pp.y]+a[pp.x]*a[Now[i]]); the                     if(isin[now[i] [pp.y| (1<<now[i])] = =false) -Q.push (P (Now[i], pp.y| (1<<now[i])), isin[now[i] [pp.y| (1<<now[i]) =true; Wu                 } -                 Else{ About                      for(intK =0; K < n; k++){ $                         if((pp.y& (1<<k)) = =0&AMP;&AMP;P[K] = =-1){ -Dp[k][pp.y| (1<<k)] = max (dp[k][pp.y| (1&LT;&LT;K)], dp[pp.x][pp.y]+a[pp.x]*a[k]); -                             if(isin[K] [pp.y| (1<<k)] = =false) -Q.push (P (k, pp.y| (1<<k)), isin[K] [pp.y| (1&LT;&LT;K)] =true; A                         } +                     } the                 } -             } $         } the  thell ans =-1e17; the          for(inti =0; I < n; i++) theans = max (ans, dp[i][(1<<n)-1 ]); -printf"Case #%d:\n", ca++); incout<<ans<<Endl; the     } the     return 0; About}
View CodeBD String

After disconnecting from a point, the latter part is folded to the front, and the second half of the previous part is exactly the same as a whole.

1#include <bits/stdc++.h>2typedefLong Longll;3 using namespacestd;4 5 ll Getsum (ll x) {6     if(x <=2)7         returnx;8 9ll fir =1;Ten      while(fir*2<=x) OneFir <<=1; A  -     return1ll+x-fir + getsum (fir-(X-fir)-1 ); - } the intMain () { -     intT, CA =1; - ll L, R; -Cin>>T; +      while(t--){ -Cin>>l>>R; +ll ans = getsum (r)-getsum (l1); Acout<<ans<<Endl; at     } -     return 0; -}
View CodeGym Class

The SB topology can be sorted.

Baidu Star Astar ROUND2A

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.