HDU 2802 F(N)

Description

给一个公式,求F(N)

Algorithm

肯定是有循环 不然O(N)都TLE,直接找循环节,然后就变成周期问题了

Hint

找循环节那个数组范围要开大,其次数据类型也要是long long

Code

#include <cmath>
#include <iostream>
using namespace std;
const int maxn = 5000;
const int kmod = 2009;
int main()
{
  int a[maxn] = {0, 1, 7};
  long long t;
  for (int i = 3; i < maxn; i++)
  {
    a[i] = (a[i - 2] - ((i - 1) * (i - 1) * (i - 1)) + (i * i * i)) % kmod;
    if (a[i] == 1)
    {
      t = i - 1;
    }
  }
  long long n;
  for (;;)
  {
    cin >> n;
    if (n == 0) break;
    if (n % t == 0) cout << a[t]; else cout << a[n % t];
    cout << endl;
  }
  return 0;
}

你可能感兴趣的:(HDU 2802 F(N))