您的位置:首页 > 其它

51nod 1103 N的倍数(抽屉原理)

2016-10-01 16:37 260 查看

1103 N的倍数

题目来源: Ural 1302
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题

一个长度为N的数组A,从A中选出若干个数,使得这些数的和是N的倍数。
例如:N = 8,数组A包括:2 5 6 3 18 7 11 19,可以选2 6,因为2 + 6 = 8,是8的倍数。

Input
第1行:1个数N,N为数组的长度,同时也是要求的倍数。(2 <= N <= 50000)
第2 - N + 1行:数组A的元素。(0 < A[i] <= 10^9)

Output
如果没有符合条件的组合,输出No Solution。
第1行:1个数S表示你所选择的数的数量。
第2 - S + 1行:每行1个数,对应你所选择的数。

Input示例
8
2
5
6
3
18
7
11
19

Output示例
2
2
6


/*
51nod 1103 N的倍数(抽屉原理)

problem:
给你n个数,求是否存在几个数和为n

solve:
因为和的是n的倍数,所以对n取模后为0.
先求出所有的前缀和对n取模, 如果为0直接就能得出答案.
取模后[1,n-1]是无解的,但是前缀和总共有n个,所以必定有两个值相等,相减后为0

hhh - 2016/10/01 16:31:04
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <functional>
#include <math.h>
#define lson  i<<1
#define rson  i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 3e6 + 1000;
const int inf = 0x3f3f3f3f;
const ll mod = 1000000007;
const double eps = 1e-7;
template<class T> void read(T&num)
{
char CH;
bool F=false;
for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p)
{
if(!p)
{
puts("0");
return;
}
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
ll a[maxn];
ll n;
int vis[maxn];
ll x[maxn];
int main()
{
read(n);
memset(vis,0,sizeof(vis));
a[0] = 0;
int l,r;
int flag = 0;
for(int i = 1;i <= n;i++)
{
read(x[i]);
a[i] = (a[i-1] + x[i]) % n;
if(flag)
continue;
if(vis[a[i]] != 0)
{
flag = 1;
l = vis[a[i]] + 1,r = i;
}
vis[a[i]] = i;
if(a[i] == 0)
{
flag = 1;
l = 1,r = i;
}
}
print(r - l + 1);
//   cout << l <<" " << r <<endl;
for(int i = l;i <= r;i++)
{
print(x[i]);
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: