Halloween treats

传送门

题目描述

给出一个含有 \(n\) 个数字的序列,要找一个连续的子序列,使他们的和一定是 \(c\) 的倍数,若不存在,则输出 no sweetws\(1 \le c \le n \le 10^5\)

思路

利用 鸽巢原理 ,可以得到,连续的 \(n\) 个整数的和的 模 \(n\) 前缀和中,\(0-n-1\) 中的数字必定至少有一个数字出现两次

CODE
/********************
Author:  Nanfeng1997
Contest: POJ - Ulm Local 2007
URL:     http://poj.org/problem?id=3370
When:    2022-03-15 19:25:46

Memory:  65MB
Time:    2000ms
********************/
#include  <iostream>
using namespace std;

const int N = 1e5 + 10;

int a[N];
int last[N];
void solve() {
  int n, m; 
  while(cin >> n >> m, n || m) {
    for(int i = 1; i <= m; i ++ ) {
      scanf("%d", &a[i]);
      last[i] = -1;
    }
    int s = 0; last[0] = 0;
    for(int i = 1; i <= m; i ++ ) {
      s += a[i]; s %= n;
      if(last[s] != -1) {
        for(int j = last[s] + 1; j <= i; j ++ ) {
          printf("%d ", j);
        }
        puts("");
        break;
      }

      last[s] = i;
    }
  }
}

int main() {
  int T = 1;// cin >> T;
  while(T --) solve();

  return 0;
}

posted @ 2022-03-15 19:47  ccz9729  阅读(44)  评论(0编辑  收藏  举报