• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 5776 sum (前缀和)

题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数。

析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得。。。。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int vis[maxn];

int main(){
   // ios::sync_with_stdio(false);
    int T, q;  cin >> T;
    while(T--){
        scanf("%d %d", &n, &m);
        LL sum = 0;
        memset(vis, 0, sizeof(vis));
        bool ok = false;
        vis[0] = 1;
        for(int i = 0; i < n; ++i){
            scanf("%d", &q);
            sum += q;
            if(vis[sum%m])  ok = true;
            vis[sum%m] = 1;
        }
        printf("%s\n", ok ? "YES" : "NO");
    }
    return 0;
}

 

posted on 2016-07-31 17:02  dwtfukgv  阅读(362)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3