美团,求大佬,被7整除(10%), 关灯泡(40%, 超时)
被7整除。
int count1(int num) { int count = 0; while (num) { num = num & (num - 1); count++; } return count; } int solve(int num, vector<string>& vec, int n) { int index1 = -1, index2 = -1; for (int i = 0; i < n; i++) { if (num & 1) { if (index1 == -1) { index1 = i; } else if (index2 == -1) { index2 = i; } else break; } num = num >> 1; } int count = 0; long long res1 = stoll(vec[index1] + vec[index2]); long long res2 = stoll(vec[index2] + vec[index1]); if (res1 % 7 == 0) count++; if (res2 % 7 == 0) count++; return count; } int main() { int n; while (~(scanf("%d", &n))) { vector<string> vec(n, ""); long long count = 0; for (int i = 0; i < n; i++) cin >> vec[i]; for (int i = 0; i < (1<<n); i++) { if (count1(i) == 2) { count += solve(i, vec, n); } } printf("%lld\n", count); } return 0; }
关灯泡
bool helperFirst(vector<int>& vec, int index) { for (int i = index; i < vec.size(); i++) { if (vec[i] == 1) vec[i] = 0; else vec[i] = 1; } bool flag = true; for (int i = 0; i < vec.size(); i++) { if (vec[i] == 1) { flag = false; break; } } if (flag) return flag; for (int i = 0; i < vec.size(); i++) { if (vec[i]) { if (helperFirst(vec, i)) { flag = false; break; } } } return flag; } int main() { int n; while (cin >> n) { vector<int> vec(n, 0); for (int i = 0; i < n; i++) cin >> vec[i]; int flag = false; for (int i = 0; i < n; i++) { if (vec[i]) { if (helperFirst(vec, i)) { flag = true; break; } } } if (flag) cout << "Alice" << endl; else cout << "Bob" << endl; } return 0; }
#美团#