原題鏈接
考察遞推關系,a[i].cas = a[i-1].cas *2; a[i].num = a[i-1].num * 2 - a[i-1].cas + a[i].cas;
附ac代碼:
#includestruct Node{ long long cas; long long num; }; int main(){ int t, n, s, i; Node a[21]; a[0].cas = a[0].num = 0; a[1].cas = a[1].num = 1; a[2].cas = 2; a[2].num = 3; for(i = 3; i != 21; ++i){ a[i].cas = a[i - 1].cas * 2; a[i].num = a[i - 1].num * 2 - a[i- 1].cas + a[i].cas; } scanf("%d", &t); while(t-- && scanf("%d", &n)) printf("%d\n", a[n].num); return 0; }