Ultraviolet A 11237-Halloween treats (Drawer principle)

Source: Internet
Author: User

Link: Ultraviolet A 11237-Halloween treats

There are c children who want to go to the neighbor's house to ask for candy. There are n neighbors who only provide a fixed number of sweets. The bear children do not conflict with each other, I decided to distribute the obtained candy evenly. I asked if I could evenly distribute the candy from those neighbors. Note that N is greater than or equal to C.

Solution concept: Drawer principle, finding the prefix and number of sequences, there are N. After the prefix and Modulo for C are obtained, the theorem of the remainder series must be in the range of 0 ~ If the value is 0, the answer is needless to say. If the prefix and the remainder of the two ends indicate that the sum of the middle section is a multiple of C. And because N is greater than or equal to C, there must be a solution for getting 0, so the number of N corresponds to the location of C-1, and there must be the same remainder.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000;int a[maxn+5], s[maxn+5], vis[maxn+5];int main () {    int c, n;    while (scanf("%d%d", &c, &n) == 2 && c + n) {        for (int i = 1; i <= n; i++)            scanf("%d", &a[i]);        memset(vis, -1, sizeof(vis));        vis[0] = s[0] = 0;        for (int i = 1; i <= n; i++) {            s[i] = (s[i-1] + a[i]) % c;            if (vis[s[i]] == -1)                vis[s[i]] = i;            else {                for (int j = vis[s[i]] + 1; j <= i; j++)                    printf("%d%c", j, j == i ? ‘\n‘ : ‘ ‘);                break;            }        }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.