zoukankan      html  css  js  c++  java
  • Mathematics:Find a multiple(POJ 2356)

      

      找组合

      题目大意:给你N个自然数,请你求出若干个数的组合的和为N的整数倍的数

      经典鸽巢原理题目,鸽巢原理的意思是,有N个物品,放在N-1个集合中,则一定存在一个集合有2个元素或以上。

      这一题是说有找出和为N的整数倍的组合,则说明目标是找到sum[i]%N==0,而sum[i]%N恰好有N-1种非0的情况,而sum有N个,那么根据鸽巢原理,一定存在i,j,使sum[i]%N==sum[j]%N,且(sum[i]-sum[j])%N==0,同时j-i就是组合的个数,而且在这里,组合必定连续(因为sum是连续递增的)。

      

     1 #include <iostream>
     2 #include <algorithm>
     3 #define MAX_N 10001
     4 
     5 using namespace std;
     6 
     7 static int hash_sum[MAX_N], num[MAX_N], sum[MAX_N];
     8 
     9 int main(void)
    10 {
    11     int sum_n;
    12     scanf("%d", &sum_n);
    13     
    14     fill(hash_sum, hash_sum + sum_n, -1);
    15     hash_sum[0] = 0;
    16 
    17     for (int i = 1; i <= sum_n; i++)
    18     {
    19         scanf("%d", &num[i]);
    20         sum[i] = (sum[i - 1] + num[i]) % sum_n;
    21         if (hash_sum[sum[i]] == -1)
    22             hash_sum[sum[i]] = i;
    23         else
    24         {
    25             printf("%d
    ", i - hash_sum[sum[i]]);
    26             for (int j = hash_sum[sum[i]] + 1; j <= i; j++)
    27                 printf("%d
    ", num[j]);
    28             break;
    29         }
    30     }
    31     return 0;
    32 }

      

      另外这一题又卡cin了,用了std::ios::sync_with_stdio(false)都不行,不知道为什么。

      参考:http://www.cnblogs.com/BlackStorm/p/5243156.html

  • 相关阅读:
    Vivian's Problem UVA
    Prime k-tuple UVA
    M
    Make a Crystal UVA
    Cryptography Reloaded UVALive
    Gauss Prime UVA
    Discrete Square Roots UVALive
    The Bells are Ringing UVALive
    Huge Mods UVA
    指数循环节
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5264065.html
Copyright © 2011-2022 走看看