Find a multiple

链接:http://acm.hust.edu.cn/vjudge/problem/17962/origin

题目:The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).

题意:给一串共n数字,求一个连续的字串使子串的和为n的倍数。

分析:利用优化的前缀和,优化的方法是前缀和数组都对n取余。这样如果存在前缀和为零或者前缀和相同就找到了字串。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define pii pair<int,int>
#define lowbit(x) x&(-x)
using namespace std;
#define maxi 0x3f3f3f3f
#define MAX 100010

int s[10010];
int sum[10010];
int rest[10010];
int main()
{
	//fr;
	detie;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> s[i];
		sum[i] = (sum[i - 1] + s[i]) % n;
	}
	for (int i = 1; i <= n; i++)
	{
		if (sum[i] == 0)
		{
			cout << i << endl;
			for (int j = 1; j <= i; j++)
				cout << s[j] << endl;
			break;
		}
		else if (rest[sum[i]] == 0)
			rest[sum[i]] = i;
		else
		{
			cout << i - rest[sum[i]] << endl;
			for (int j = rest[sum[i]] + 1; j <= i; j++)
				cout << s[j] << endl;
			break;
		}
	}
	rt 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值