转自ACMore_Xiong,时刻提醒自己多想到模运算

思维题:抽屉原理 hdu 5776 sum & 51Nod 1103 N的倍数

题目链接:   hdu 5776 sum 、  51nod 1103 N的倍数
hdu5576 sum题意:给定长度为N的整数序列,问该序列是否存在一个连续的子区间的和为M的倍数。 (1≤n≤100000, 1≤m≤5000).
51nod 1103 N的倍数 题意: 一个长度为N的数组A,从A中选出若干个数,使得这些数的和是N的倍数。(2 <= N <= 50000)
思路:两个题目思路一样。
以51Nod 1103 为例。先求出前缀和 % N。
1.如果这些和中有一个0,那么我们便得到所求。
2.否则,这些和中必有两个是相等的(抽屉原理),他们相减为0,这便又找到我们想要的。利用抽屉原理,因为N的倍数,(除去第一种情况之后),A[i]中的任意一个数modN 都是在[1, N-1] 然后把N个数扔到这些个N-1个格子里,必定会有两个一样,这两个做减法就是一样的了。
那么其实可以发现,不可能存在结果为“No Solution” 的情况。

hdu 5776 sum这题也是求出前缀和 % M。

如果出现前缀和相等的情况,证明答案为YES, 否则答案为NO。


/**
 * 51Nod 1103 N的倍数
 */
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64  LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 50000 + 5;

int N;
int A[MAXN], SUM[MAXN];
int vis[MAXN];

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    while(~scanf("%d", &N)) {
        memset(vis, -1, sizeof(vis));
        SUM[0] = 0;
        for(int i = 1; i <= N; i++) {
            scanf("%d", &A[i]);
            SUM[i] = (SUM[i - 1] + A[i]) % N;
        }
        bool suc = false;
        int a, b, cnt;
        vis[0] = 0;
        for(int i = 1; i <= N; i++) {
            if(~vis[SUM[i]]) {
                a = vis[SUM[i]] + 1;
                b = i;
                cnt = b - a + 1;
                suc = true;
                break;
            } else {
                vis[SUM[i]] = i;
            }
        }
        if(!suc) {
            printf("No Solution\n"); // 其实这种情况不可能存在
            continue;
        } else {
            printf("%d\n", cnt);
            for(int i = a; i <= b; i++) {
                printf("%d\n", A[i]);
            }
        }
    }
    return 0;
}

/**
 * Hdu 5776 sum
 */
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
#define lson            l, mid, rt << 1
#define rson            mid + 1, r, rt << 1 | 1

typedef __int64  LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int MAXN = 100000 + 5;
const int MAXM = 5000 + 5;
int T, N, M;
int A[MAXN];
int SUM[MAXN];
bool vis[MAXM];
int main() {
#ifndef ONLINE_JUDGE
    FIN;
    // FOUT;
#endif // ONLINE_JUDGE
    scanf("%d", &T);
    while (T--) {
        scanf("%d %d", &N, &M);
        bool suc = false;
        memset(vis, false, sizeof(vis));
        vis[0] = true;
        SUM[0] = 0;
        for (int i = 1; i <= N; i++) {
            scanf("%d", &A[i]);
            if (suc) continue;
            SUM[i] = (SUM[i - 1] + A[i]) % M;
            if (vis[SUM[i]]) suc = true;
            else vis[SUM[i]] = true;
        }
        puts(suc ? "YES" : "NO");
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值