#include<cstdio> #include<vector> #include<cstring> usingnamespace std; typedef vector<vector<longlong> /**/> mat; mat unit = mat{vector<longlong>{1, 0}, vector<longlong>{0, 1}}; mat zero = mat{vector<longlong>{0, 0}, vector<longlong>{0, 0}}; int a, b, x1, x2, mod; char n[1000000]; const mat operator*(const mat &a, const mat &b) { mat ret = zero; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) ret[i][j] += a[i][k] * b[k][j]; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) ret[i][j] %= mod; return ret; } template <typename T> const T operator^(T a, int n) { T ret = unit; while (n) { if (n & 1) ret = ret * a; n >>= 1; a = a * a; } return ret; } intmain(){ scanf("%d %d %d %d", &x1, &x2, &a, &b); scanf("%s %d", n, &mod); int length = strlen(n); mat res = unit; mat base = mat{vector<longlong>{a, b}, vector<longlong>{1, 0}}; for (int i = length - 1; i >= 0; i--) { if (n[i] - '0') res = res * (base ^ (n[i] - '0')); base = base ^ 10; } res = res * mat{vector<longlong>{x2, 0}, vector<longlong>{x1, 0}}; printf("%lld\n", res[1][0]); }
T = int(input()) case = 0 while T > case: case += 1 x = input() x = [ int(x[i:i+16], 2) for i inrange( 0, len(x), 16 )] s = [':'.join([hex(i)[2:] for i in x])] for i inrange(8): for j inrange(i+1, 8): flag = True for k in x[i:j+1]: if k != 0: flag = False if flag: s.append(':'.join([hex(_)[2:] for _ in x[:i]])+'::'+ ':'.join([hex(_)[2:] for _ in x[j+1:]])) s.sort(key=lambda x: (len(x), x)) print('Case #%d:' % (case), s[0])
但是python里头有个all,可以判断一个可遍历对象里头是否都为true
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
T = int(input()) case = 0 while T>case: case+=1 x = input() x = [hex(int(x[i:i+16], 2))[2:] for i inrange(0, len(x), 16) ] s = [':'.join(x)] for i inrange(8): for j inrange(i+1,8): ifall(map(lambda x:x=='0',x[i:j+1])): s.append(':'.join(x[:i])+'::'+':'.join(x[j+1:]) ) s.sort(key=lambda x:(len(x),x)) print('Case #%d:'%(case),s[0])