1. 程式人生 > >PAT NowCoder數列 詳細題解 (思維水題)

PAT NowCoder數列 詳細題解 (思維水題)

 

最近準備打一次PAT試試, 之前感覺乙級題目也就那樣, 現在一刷發現還是有不少很有難度的好題的

比如這一題, 看似很簡單, 其實也蘊含了一點點思維技巧

如果僅僅用斐波那契數列來寫遞推的話, 一定會超時, 那麼一般的水題就是找規律啦, icpc裡一般水題也就是這樣的題

首先我們考慮一下前兩個7, 11這兩個數 2*3+1, 3*3+2

那麼下一個肯定就是5*3 + 3, 也就是題目中要求的3的倍數啦

那麼我們對於每個數, 都只考慮他%3之後的數, 因為夠3就可以忽略這個倍數了

那麼可以發現:

也就是3的倍數只出現在 (i+2)%4==0時

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 1e6+10;


int main()
{
    int n;
    while(cin >> n){
        if((n+2)%4 == 0) cout << "Yes\n";
        else cout << "No\n";
    }
    return 0;
}