"Dfs/bfs+set+ fast Power" Swjtuoj 2094

Source: Internet
Author: User

"Dfs/bfs+set+ fast Power" Swjtuoj 2094

"Note: Jiaotong University to see this article to learn to write their own, do not play for the game!" ~

Main topic

Question one: The protagonist goes on vacation, ask after a^b days is the day of the week (simple question)
Question two: a balance, n weights, the weight of each object WI is known, ask how many of the weight can be weighed?



The second thing to note is that the weights can be placed on both sides of the balance, and each weight value can be assigned a value of w,0,-w, which is equivalent to three points, and we know that the binary can be enumerated with the bits operation, for example: enumerating all subsets

 int  j,k,top=0 ;    int  t = 1  << N; for  (int  i = 0 ; i < T; + + i)        {// bit Operation enumeration subset, O (N*2  ^n) j = i;        K = 0 ; while  (j) {if  (J&1 ) prin          TF  ( "%d " , W[k]);          J >>= 1 ;        + + K;    } printf  (); }

This three-point problem directly search, the parent function can not solve (w too big to save), search Dfs and BFS, very naked search, to reuse the set artifact! O (∩_∩) o~

"Note" and negative is also valid, after the ABS thrown into the set inside ~ ~ ~

Talk about the idea.
    • Problem one: integer fast power, the title 1<=a,b<=10 wrong, A, a a large range of
    • Question two: And before the idea of the same, direct search, I wrote two version, for the reader's reference, and thrown into the set container inside the automatic to heavy, the final output set of the size and the last value (automatic sorting after the last number is definitely the sum of the weight of all objects W)

The comparison of set and map: Map is the biggest advantage of the map, for the storage of digital sequences, there are automatic deduplication, automatic sorting function, you can also map the number of occurrences (in the range of It->second); set is a non-repeatable multi-set, the biggest feature is to go to heavy and the default sort. In short, go to the heavy artifact set orz~

Reference Code one: DFS
/*====================================*|* set de-weight + three state enumeration subset: DFS + bfs*| | * Two states can be solved with bits operation *|\*====================================*//*author:hacker_vision*/#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <map>#include <vector>#include <queue>#include <set>#define CLR (k,v) memset (k,v,sizeof (k) )#define LL Long Long#define EPS 1e-8using namespace  STD;Const int_max =4e6+Ten;Const intMoD =7;inta,b,n,w[ -];structnode{intNum ll sum;}; set<ll>SvoidDfsintDeep,ll tot) {//Depth-First search if(deep==n) {if(TOT) S.insert (ABS(tot));return; } DFS (deep+1, tot); DFS (deep+1, Tot-w[deep]); DFS (deep+1, Tot+w[deep]);} ll Quick_mod (ll A,ll b) {//Matrix fast powerll ans =1; while(b) {if(b&1) ans = (ans*a)% MoD; b >>=1;  A = (A * a)% mod; }returnAns;}intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge    while(scanf("%d%d", &a,&b) = =2) {ll d = quick_mod (LL) A, (ll) b);if(d%7==0|| d%7==6)printf(" the");Else printf("+");scanf("%d", &n); for(inti =0; I < n; + + i) {scanf("%d", w+i);    } s.clear (); Dfs0,0); set<ll>:: Reverse_iterator it = S.rbegin ();//Reverse iterator    printf("%d%lld\n", S.size (), *it); }return 0;}
Reference Code two: BFS
/*====================================*|* set de-weight + three state enumeration subset: DFS + bfs*| | * Two states can be solved with bits operation *|\*====================================*//*author:hacker_vision*/#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <map>#include <vector>#include <queue>#include <set>#define CLR (k,v) memset (k,v,sizeof (k) )#define LL Long Long#define EPS 1e-8using namespace  STD;Const int_max =4e6+Ten;Const intMoD =7;inta,b,n,w[ -];structnode{intNum ll sum;}; set<ll>S queue<node>Q;voidBFS () {//Breadth-First searchNode q; Q.num =0; Q.sum =0; Q.push (q); while(!    Q.empty ()) {q = Q.front (); Q.pop ();if(q.num==n) {if(q.sum>0) S.insert (q.sum);Continue;//return; The shortest circuit is return.} node P;p.num = Q.num +1; for(inti =0; I <3; + + i) {if(i = =0) {p.sum = Q.sum; }Else if(i = =1) {p.sum = Q.sum + W[q.num]; }ElseP.sum = Q.sum-w[q.num];    Q.push (P); }}}ll Quick_mod (ll a,ll b) {//Matrix fast powerll ans =1; while(b) {if(b&1) ans = (ans*a)% MoD; b >>=1;  A = (A * a)% mod; }returnAns;}intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge    while(scanf("%d%d", &a,&b) = =2) {ll d = quick_mod (LL) A, (ll) b);if(d%7==0|| d%7==6)printf(" the");Else printf("+");scanf("%d", &n); for(inti =0; I < n; + + i) {scanf("%d", w+i); } s.clear (); while(!    Q.empty ()) Q.pop (); BFS (); set<ll>:: Reverse_iterator it = S.rbegin ();//Reverse iterator    printf("%d%lld\n", S.size (), *it); }return 0;}
    • Bold Ctrl + B
    • Italic Body Ctrl + I
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Dfs/bfs+set+ fast Power" Swjtuoj 2094

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.