// 第二題O(n)空間 #if 0 typedef long long ll; int main() { ll n; while (cin >> n) { if (n < 5) { cout << 1 << endl; continue; } vector<ll> dp(n + 1); dp[0] = dp[1] = dp[2] = dp[3] = dp[4] = 1; for (ll i = 5; i <= n; i++) { dp[i] = 2018 * dp[i - 1] % 1000000003 + 2017 * dp[i - 2] % 1000000003 + 2016 * dp[i - 3] % 1000000003 + 2015 * dp[i - 4] % 1000000003 + 2014 * dp[i - 5] % 1000000003; } cout << dp[n] % 1000000003 << endl; } return 0; } #endif // 第二題 時間複雜度超時 #if 0 typedef long long ll; inline ll ksc(ll x, ll y, ll mod) { return (x*y - (ll)((long double)x / mod*y)*mod + mod) % mod; } int main() { ll n; while (scanf("%lld",&n)!=EOF) //while (cin >> n) { if (n < 5) { //cout << 1 << endl; printf("1\n"); continue; } vector<ll> dp(6); dp[0] = dp[1] = dp[2] = dp[3] = dp[4] = 1; // 使用6個空間,而後每次都須要移位 for (ll i = 5; i <= n; i++) { dp[5] = 2018 * (dp[4] % 1000000003) + 2017 * (dp[3] % 1000000003) + 2016 * (dp[2] % 1000000003) + 2015 * (dp[1] % 1000000003) + 2014 * (dp[0] % 1000000003); for (int j = 0; j < 5; j++) { dp[j] = dp[j + 1]; } } printf("%lld\n", dp[5] % 1000000003); // 不用移位 //for (ll i = 5; i <= n; i++) //{ // dp[i % 6] = ksc(2018, dp[(i - 1) % 6], 1000000003) + ksc(2017, dp[(i - 2) % 6], 1000000003) + ksc(2016, dp[(i - 3) % 6], 1000000003) + ksc(2015, dp[(i - 4) % 6], 1000000003) + ksc(2014, dp[(i - 5) % 6], 1000000003); // //dp[i % 6] = 2018 * (dp[(i - 1) % 6] % 1000000003) + 2017 * (dp[(i - 2) % 6] % 1000000003) + 2016 * (dp[(i - 3) % 6] % 1000000003) + 2015 * (dp[(i - 4) % 6] % 1000000003) + 2014 * (dp[(i - 5) % 6] % 1000000003); //} ////cout << dp[n%6] % 1000000003 << endl; //printf("%lld\n", dp[n % 6] % 1000000003); } return 0; } #endif // 第二題 時間複雜度超時 #if 0 typedef long long ll; long long quick_multiply(long long a, long long b, long long mod) { long long result = 0; while (b) { result = (result + (b % 2 * a) % mod) % mod; a = a * 2 % mod; b = b / 2; } return result; } inline ll ksc(ll x, ll y, ll mod) { return (x*y - (ll)((long double)x / mod*y)*mod + mod) % mod; } int main() { ll n; while (scanf("%lld", &n) != EOF) //while (cin >> n) { if (n < 5) { //cout << 1 << endl; printf("1\n"); continue; } vector<ll> dp(6); dp[0] = dp[1] = dp[2] = dp[3] = dp[4] = 1; for (ll i = 5; i <= n; i++) { dp[i % 6] = quick_multiply(2018, dp[(i - 1) % 6], 1000000003) + quick_multiply(2017, dp[(i - 2) % 6], 1000000003) + quick_multiply(2016, dp[(i - 3) % 6], 1000000003) + quick_multiply(2015, dp[(i - 4) % 6], 1000000003) + quick_multiply(2014, dp[(i - 5) % 6], 1000000003); //dp[i % 6] = 2018 * (dp[(i - 1) % 6] % 1000000003) + 2017 * (dp[(i - 2) % 6] % 1000000003) + 2016 * (dp[(i - 3) % 6] % 1000000003) + 2015 * (dp[(i - 4) % 6] % 1000000003) + 2014 * (dp[(i - 5) % 6] % 1000000003); } //cout << dp[n%6] % 1000000003 << endl; printf("%lld\n", dp[n % 6] % 1000000003); } return 0; } #endif #if 0 // 快速冪運算求解斐波拉契數列 #include <cstdio> #include <iostream> using namespace std; const int MOD = 10000; struct matrix { int m[2][2]; }ans, base; matrix multi(matrix a, matrix b) { matrix tmp; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) { tmp.m[i][j] = 0; for(int k = 0; k < 2; ++k) tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD; } } return tmp; } int fast_mod(int n) // 求矩陣 base 的 n 次冪 { base.m[0][0] = base.m[0][1] = base.m[1][0] = 1; base.m[1][1] = 0; ans.m[0][0] = ans.m[1][1] = 1; // ans 初始化爲單位矩陣 ans.m[0][1] = ans.m[1][0] = 0; while (n) { if (n & 1) //實現 ans *= t; 其中要先把 ans賦值給 tmp,而後用 ans = tmp * t { ans = multi(ans, base); } base = multi(base, base); n >>= 1; } return ans.m[0][1]; } int main() { int n; while (scanf("%d", &n) && n != -1) { printf("%d\n", fast_mod(n)); } return 0; } #endif //計算(x^y) % N; 注:(x^y)表示x的y次方 #if 0 int main() { int x, y, N; cin >> x >> y >> N; long res = 1; x = x % N; //開始 while (y > 0) { if (y % 2 == 1) //等價於 if(y&1) res = (res * x) % N; y /= 2; //y>>1; 分解y爲二進制編碼 x = (x * x) % N; } cout << res << endl; return 0; } #endif
// 第一題 #if 0 int main() { int N, M; cin >> N >> M; unordered_set<string> st; for (int i = 0; i < N;i++) { string temp; cin >> temp; st.insert(temp); } for (int i = 0; i < M;i++) { string temp; cin >> temp; if (st.find(temp)!=st.end()) //找到了 { cout << "NO" << endl; } else { st.insert(temp); cout << "YES" << endl; } } return 0; } #endif