1.大搬家
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1000000 + 10; const int mod = 1000000007; int n, cnt; long long dp[maxn]; void solve(){ dp[0] = 0; dp[1] = 1; dp[2] = 2; dp[3] = 4; for(int i = 3; i <= maxn; i++){ dp[i] = (dp[i - 1] + ((i - 1) * dp[i - 2])) % mod; } } int main(){ int _T, cnt = 0; scanf("%d", &_T); solve(); while(_T--){ scanf("%d", &n); printf("Case #%d:\n%I64d\n", ++cnt, dp[n]); } return 0; }
2.列变位法解密
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100000 + 10; int k, kase = 1; char str[maxn], ans_str[maxn]; void solve(){ int row, col; int len = strlen(str); int n = ceil(1.0 * len / k); int mark = len % k, cnt = 0; for(int j = 0; j < k; j++){ if(mark != 0 && j == mark){ n--; } for(int i = 0; i < n; i++){ ans_str[i * k + j] = str[cnt++]; } } ans_str[len] = '\0'; printf("Case #%d:\n%s\n", kase++, ans_str); } int main(){ //freopen("5-11-I.txt", "r", stdin); int _T; scanf("%d", &_T); while(_T--){ getchar(); gets(str); scanf("%d", &k); solve(); } return 0; }
3. IP聚合
#include <map> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1000 + 10; const int maxm = 50 + 5; long long IP[maxn], Son[maxm]; int Pow[5] = {1, 10, 100, 1000, 10000}; int kase = 1, n, m; char str[20]; long long predo(char * str){ long long res = 0; long long temp = 0; int cnt = 0, mark = 1; int pos = strlen(str) - 1; while(pos >= 0){ if(str[pos] == '.'){ res += temp * mark; temp = 0, cnt = 0; mark <<= 8; } else { temp += (str[pos] - '0') * Pow[cnt++]; } pos--; } res += temp * (mark); return res; } void init(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++){ scanf("%s", str); IP[i] = predo(str); } for(int i = 1; i <= m; i++){ scanf("%s", str); Son[i] = predo(str); } } void solve(){ long long temp; printf("Case #%d:\n", kase++); for(int i = 1; i <= m; i++){ map<long long, int> MM; for(int j = 1; j <= n; j++){ temp = Son[i] & IP[j]; MM[temp] = 1; } printf("%d\n", MM.size()); } } int main(){ int _T; scanf("%d", &_T); while(_T--){ init(); solve(); } return 0; }