發表文章

目前顯示的是有「暴搜」標籤的文章

[Codeforces] 894D. Ralph And His Tour in Binary Country

題目連結: http://codeforces.com/problemset/problem/894/D 看了題解又問了人才會這題QAQ 本題作法我覺得好暴力(?,首先要觀察出這題第$i$條邊固定連接$ \lfloor \frac{i+1}{2} \rfloor $跟$i+1$的話,那給定的這棵樹就一定會是一棵完滿二元樹,深度必定為$\log_2(n)$,接著我們要預處理這棵樹,把每個點到其子樹們(包含自己)的距離算出來並排序好(這裡可以用merge sort的merge方法把兩棵子樹merge好),那我們要查詢的時候就只要往上走,並把我另外一邊的子樹可以走到的節點們通通加起來就好了。 #include <bits/stdc++.h> using namespace std; typedef long long lld; #define PB push_back #define SZ(x) ((int)(x).size()) #define ALL(x) begin(x), end(x) typedef pair<lld,int> PLI; #define FF first #define SS second const int N = 1000000 + 5; vector<lld> dis[N], sum[N]; lld len[N]; void build(int,int); lld go(int,lld,int); PLI get_val(int,lld); int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n, q; cin>>n>>q; for(int i=2;i<=n;i++) cin>>len[i]; build(1, n); while(q--){ int w, l; cin>>w>>l; cout<...

[TIOJ] 1025. 數獨問題

題目連結: http://tioj.infor.org/problems/1025 又是一題數獨,同樣的按照對的順序dfs,則你輸出答案的順序也會是好的,所以就爆搜吧XD #include <bits/stdc++.h> using namespace std; struct pos{ int x,y; pos(int a,int b){x=a;y=b;} }; vector<pos> need; int sudoku[9][9]; int sum=0; void solve(int); inline bool isOK(); int main(){ ios_base::sync_with_stdio(0);cin.tie(0); for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ cin>>sudoku[i][j]; if(sudoku[i][j]==0)need.push_back(pos(i,j)); } } solve(0); cout<<"there are a total of "<<sum<<" solution(s)."; return 0; } void solve(int w){ if(w==need.size()){ for(int i=0;i<9;i++){ for(int j=0;j<9;j++)cout<<sudoku[i][j]<<' '; cout<<'\n'; } cout<<'\n'; ...

[TIOJ] 1235. 富翁種花

題目連結: http://tioj.infor.org/problems/1235 比較trivial的數獨,轉成正常數獨後,就暴搜吧XD #include <bits/stdc++.h> using namespace std; struct pos{ int x,y; pos(){} pos(int a,int b){x=a;y=b;} }; int sudoku[9][9]; bitset<9> ori[9]; vector<pos> need; bool solve(int); inline bool check(int,int); inline int toid(char); inline char toch(char); int main(){ char ss[15]; for(int i=0;i<9;i++){ gets(ss); for(int j=0;j<9;j++){ if(ss[j]=='*'){ sudoku[i][j]=0; need.push_back(pos(i,j)); ori[i][j]=1; }else{ sudoku[i][j] = toid(ss[j]); ori[i][j]=0; } } } solve(0); return 0; } inline int toid(char a){ switch(a){ case 'R':return 1; case 'O':return 2;...

[TIOJ] 1823. 幸運碼

題目連結: http://tioj.infor.org/problems/1823 本地建表暴搜亂搞題(?,可以發現若是那個數無法用這些操作變成一,那必然他是會循環的,所以直接開個表看他有沒有重複就好,附個暴搜用的python code: def modify(x,l): if x in l: return False length = len(str(x)) l.append(x) if x == 1: return True elif length == 1: return modify(x**2,l) else: temp=0 for k in str(x): temp += int(k)**2 return modify(temp,l) count = 0 test = 1 while count <= 10000: if modify(test,[]): count+=1 if count in [10,50,100,1000,10000]: print(test) test+=1 Main Code: main(){write(1,"44\n320\n694\n6899\n67169\n",22);exit(0);}