程序代码:
#include <stdio.h> #define MAX 10000 int main() { int N = 8; size_t A[MAX] = {2, 5, 6, 3, 18, 7, 11, 19}; size_t sum = 0, sa[MAX + 1] = {0}, ss[MAX][2] = {1}; // ss 记录sum第一次和第二次出现的位置 for (size_t i = 0; i < N; ++i) { sum = (sum + A[i]) % N; sa[i + 1] = sum; if (ss[sum][1]) continue; ss[sum][ss[sum][0] != 0] = i + 2; } for (size_t i = 0; i < N; ++i) { if (ss[sa[i]][1]) { printf("%zu\n%zu\n", ss[sa[i]][1] - ss[sa[i]][0], i + 1); break; } } return 0; }
[此贴子已经被作者于2016-12-23 23:01编辑过]
[fly]存在即是合理[/fly]