【扬中集训 DAY4T1】跳马

【题目链接】

          点击打开链接

【算法】

        数据范围很大,显然暴力是不能通过的

        我们可以先打表,发现答案为 :

        41 109 205 325 473 649 853 1085 1345

        观察数列的差

        68 116 120 148 176....

        发现数列的差的差在第5项后每次差28,因此我们推出公式

        ans(n) = 325 + 148(n-5) + 14(n-5)(n-6)

【代码】

         

#include<bits/stdc++.h>
using namespace std;

unsigned long long T,N,ans;
unsigned long long val[5] = {1,41,109,205,325};

template <typename T> void read(T &x) {
        int f=1; char c = getchar(); x=0;
        for (; !isdigit(c); c = getchar()) { if (c=='-') f=-1; }
        for (; isdigit(c); c = getchar()) x=x*10+c-'0';
        x*=f;
}

template <typename T> inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x/10);
    putchar(x%10+'0');    
}

template <typename T> inline void writeln(T x) {
    write(x);
    puts("");    
}

int main() {
        
        read(T);
        while (T--) {
                read(N);
                ans = 325 + 148 * (N - 5) + 14 * (N - 5) * (N - 6);
                if (N <= 4) writeln(val[N]);
                else writeln(ans);    
        }
        
        return 0;
    
}

/*
  41 109 205 325 473 649 853 1085 1345
  68 116 120 148 176
  325 + 148(n-5) + 14(n-5)(n-6)
*/


原文地址:https://www.cnblogs.com/evenbao/p/9196434.html