"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
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Dfs/bfs+set+ fast Power" Swjtuoj 2094