HDU 2802 F(N) [Ad Hoc]

来源:互联网 发布:java正则表达式教程 编辑:程序博客网 时间:2024/05/18 10:58

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;}
0 0
原创粉丝点击