对于递推式 (a_n = (sum_{i = 1}^{n-1}a_i * i)\%n)
打表列出 (a_i) 的前 (100) 项,发现有以 (6) 为循环的规律,具体规律见代码。
复杂度为 (O(1)) 。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
int t;
long long x;
long long solve(long long x)
{
if(x % 6 == 0 || x % 6 == 2) return x / 2;
if(x % 6 == 1) return x / 6 * 4 + 1;
if(x % 6 == 3 || x % 6 == 5) return x / 6;
if(x % 6 == 4) return x - 1;
}
int main()
{
scanf("%d", &t);
for(int cas = 1; cas <= t; cas++){
scanf("%lld", &x);
printf("%lld
", solve(x));
}
return 0;
}