标签:
4423:Necklace
一道计数问题,当时不会敲,先留着
4424:Permutations
签到题
1 /***Good Luck***/ 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include <stack> 10 #include <map> 11 #include <queue> 12 #include <vector> 13 #include <set> 14 #include <functional> 15 #include <cmath> 16 #include <numeric> 17 18 #define ll long long 19 #define Zero(x) memset((x),0, sizeof(x)) 20 #define Neg(x) memset((x), -1, sizeof(x)) 21 #define dg(x) cout << #x << " = " << x << endl 22 #define pk(x) push_back(x) 23 #define pok() pop_back() 24 #define pii pair<int, int> 25 using namespace std; 26 int OK = 1; 27 bool debug = false; 28 void get_val(int &a) { 29 int value = 0, s = 1; 30 char c; 31 while ((c = getchar()) == ‘ ‘ || c == ‘\n‘); 32 if (c == ‘-‘) s = -s; else value = c - 48; 33 while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) 34 value = value * 10 + c - 48; 35 a = s * value; 36 } 37 const int maxn = 8000100; 38 int mod = 1000000007; 39 ll arr[maxn] = {0}; 40 int main(){ 41 //freopen("data.out", "w", stdout); 42 //freopen("data.in", "r", stdin); 43 //cin.sync_with_stdio(false); 44 for(int i = 0; i < maxn; ++i){ 45 arr[i] = arr[i- 1] * i + i; 46 arr[i] %= mod; 47 } 48 int T; 49 cin >> T; 50 int n; 51 while(T--){ 52 cin >> n; 53 cout << arr[n] << endl; 54 } 55 return 0; 56 }
4426:Counting_3
f(3k + 1) = f(3k + 2) = f(k)
f(3k) = f(k) + f(k - 1)
由上公式,可推出 x = p3^y f(x) = f(p) + y(p - 1)
还要注意记忆化搜索
1 /***Good Luck***/ 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include <stack> 10 #include <map> 11 #include <queue> 12 #include <vector> 13 #include <set> 14 #include <functional> 15 #include <cmath> 16 #include <numeric> 17 18 #define ll long long 19 #define Zero(x) memset((x),0, sizeof(x)) 20 #define Neg(x) memset((x), -1, sizeof(x)) 21 #define dg(x) cout << #x << " = " << x << endl 22 #define pk(x) push_back(x) 23 #define pii pair<int, int> 24 using namespace std; 25 int OK = 1; 26 bool debug = true; 27 void get_val(ll &a) { 28 ll value = 0, s = 1; 29 char c; 30 while ((c = getchar()) == ‘ ‘ || c == ‘\n‘); 31 if (c == ‘-‘) s = -s; else value = c-48; 32 while ((c = getchar()) >= ‘0‘ && c <= ‘9‘) 33 value = value * 10 + c - 48; 34 a = s * value; 35 } 36 const int maxn = 10000000; 37 int ans[maxn] = {0}; 38 int dfs(ll v){ 39 if(v <= 0) return 1; 40 while(v % 3){ 41 v -= v % 3; 42 v /= 3; 43 } 44 if(v == 0) return 1; 45 int c = 0; 46 while(v % 3 == 0){ 47 v /= 3; 48 c++; 49 } 50 if(v < maxn && !ans[v - 1]) 51 ans[v - 1] = dfs(v - 1); 52 53 if(v < maxn && !ans[v]) ans[v] = dfs(v); 54 if(v > maxn) { 55 return c * dfs(v - 1) + dfs(v); 56 } 57 return ans[v] + c * ans[v - 1]; 58 } 59 int main() { 60 //freopen("data.out", "w", stdout); 61 //freopen("data.in", "r", stdin); 62 //cin.sync_with_stdio(false); 63 int T; 64 scanf("%d", &T); 65 66 ll n; 67 while(T--){ 68 get_val(n); 69 printf("%d\n", dfs(n)); 70 } 71 72 }
4427:Miss Zhao‘s Graph
cf上原题, dp + 贪心
1 /***Good Luck***/ 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include <stack> 10 #include <map> 11 #include <queue> 12 #include <vector> 13 #include <set> 14 #include <functional> 15 #include <cmath> 16 17 18 #define Zero(a) memset(a, 0, sizeof(a)) 19 #define Neg(a) memset(a, -1, sizeof(a)) 20 #define All(a) a.begin(), a.end() 21 #define PB push_back 22 #define inf 0x3f3f3f3f 23 #define inf2 0x7fffffffffffffff 24 #define ll long long 25 using namespace std; 26 //#pragma comment(linker, "/STACK:102400000,102400000") 27 const int maxn = 300005; 28 int dp[maxn], dp2[maxn]; 29 int rec[maxn]; 30 int n, e; 31 struct node { 32 int u, v; 33 int w; 34 }edge[maxn]; 35 36 37 bool cmp(node a, node b) { 38 return a.w < b.w; 39 } 40 int main() { 41 //freopen("data.out", "w", stdout); 42 //freopen("data.in", "r", stdin); 43 //cin.sync_with_stdio(false); 44 int T; 45 cin >> T; 46 while(T--){ 47 Zero(dp); 48 Zero(dp2); 49 scanf("%d%d", &n, &e); 50 int u, v, w; 51 for (int i = 0; i < e; ++i) { 52 scanf("%d%d%d", &u, &v, &w); 53 edge[i].u = u; 54 edge[i].v = v; 55 edge[i].w = w; 56 } 57 sort(edge, edge + e, cmp); 58 int j; 59 for (int i = 0; i < e; ++i) { 60 for (j = i; j < e && edge[j].w == edge[i].w; ++j); 61 62 for (int k = i; k < j; ++k) { 63 dp2[edge[k].v] = max(dp2[edge[k].v], dp[edge[k].u] + 1); 64 } 65 for (int k = i; k < j; ++k) { 66 dp[edge[k].v] = dp2[edge[k].v]; 67 } 68 i = j - 1; 69 } 70 int ans = 0; 71 for (int i = 1; i <= n; ++i) { 72 ans = max(ans, dp[i]); 73 } 74 cout << ans << endl; 75 } 76 return 0; 77 }
4429:frog‘s dice
二分图多重匹配题, 把最后的匹配串相同字符合并,次数+1
1 /***Good Luck***/ 2 #define _CRT_SECURE_NO_WARNINGS 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include <stack> 10 #include <map> 11 #include <queue> 12 #include <vector> 13 #include <set> 14 #include <functional> 15 #include <cmath> 16 17 18 #define Zero(a) memset(a, 0, sizeof(a)) 19 #define Neg(a) memset(a, -1, sizeof(a)) 20 #define All(a) a.begin(), a.end() 21 #define PB push_back 22 #define inf 0x3f3f3f3f 23 #define inf2 0x7fffffffffffffff 24 #define ll long long 25 using namespace std; 26 //#pragma comment(linker, "/STACK:102400000,102400000") 27 const int maxn = 300005; 28 int dp[maxn], dp2[maxn]; 29 int rec[maxn]; 30 int n, e; 31 struct node { 32 int u, v; 33 int w; 34 }edge[maxn]; 35 36 37 bool cmp(node a, node b) { 38 return a.w < b.w; 39 } 40 int main() { 41 //freopen("data.out", "w", stdout); 42 //freopen("data.in", "r", stdin); 43 //cin.sync_with_stdio(false); 44 int T; 45 cin >> T; 46 while(T--){ 47 Zero(dp); 48 Zero(dp2); 49 scanf("%d%d", &n, &e); 50 int u, v, w; 51 for (int i = 0; i < e; ++i) { 52 scanf("%d%d%d", &u, &v, &w); 53 edge[i].u = u; 54 edge[i].v = v; 55 edge[i].w = w; 56 } 57 sort(edge, edge + e, cmp); 58 int j; 59 for (int i = 0; i < e; ++i) { 60 for (j = i; j < e && edge[j].w == edge[i].w; ++j); 61 62 for (int k = i; k < j; ++k) { 63 dp2[edge[k].v] = max(dp2[edge[k].v], dp[edge[k].u] + 1); 64 } 65 for (int k = i; k < j; ++k) { 66 dp[edge[k].v] = dp2[edge[k].v]; 67 } 68 i = j - 1; 69 } 70 int ans = 0; 71 for (int i = 1; i <= n; ++i) { 72 ans = max(ans, dp[i]); 73 } 74 cout << ans << endl; 75 } 76 return 0; 77 }
标签:
原文地址:http://www.cnblogs.com/yeahpeng/p/4438406.html