BestCoder Round #85

sum

Accepts: 640
Submissions: 1744
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
Problem Description

Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. output YES, otherwise output NO

Input

The first line of the input has an integer T (1≤T≤101 \leq T \leq 101T10), which represents the number of test cases. For each test case, there are two lines: 1.The first line contains two positive integers n, m (1≤n≤1000001 \leq n \leq 1000001n100000, 1≤m≤50001 \leq m \leq 50001m5000). 2.The second line contains n positive integers x (1≤x≤1001 \leq x \leq 1001x100) according to the sequence.

Output

Output T lines, each line print a YES or NO.

Sample Input
2
3 3
1 2 3
5 7
6 6 6 6 6
Sample Output
YES
NO

题意:求字符串中是否存在连续子串和为m的倍数,是则输出"YES“,否则输出”NO“

分析:
维护前缀和,当时没想到,太弱,如果当前模得到0或者两个莫数据相同则YES,否则NO,可以用抽屉原理,(N>=M)->YES

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;
const int maxn = 1e5+10;

bool used[maxn];

int main()
{
    int n, m;
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        memset(used, 0, sizeof(used));
        used[0] = true;
        int sum = 0,x;
        bool flag = false;
        if(n>=m)
        {
            flag=1;
            for(int i=0;i<n;i++) scanf("%d",&x);
        }
        else
        {
            for(int i = 0; i < n; ++ i)
            {
                scanf("%d", &x);
                sum += x;
                sum %= m;
                if(used[sum]) {
                    flag=true;
                }
                else used[sum] = true;
            }
        }
        printf("%s\n", flag ? "YES" : "NO");
    }
    return 0;
}
/*
2
3 3
3 1 1
3 7 
6 6 9
*/

domino

Accepts: 462
Submissions: 1498
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
Problem Description

Little White plays a game.There are n pieces of dominoes on the table in a row. He can choose a domino which hasn't fall down for at most k times, let it fall to the left or right. When a domino is toppled, it will knock down the erect domino. On the assumption that all of the tiles are fallen in the end, he can set the height of all dominoes, but he wants to minimize the sum of all dominoes height. The height of every domino is an integer and at least 1.

Input

The first line of input is an integer T ( 1≤T≤101 \leq T \leq 10 1T10) There are two lines of each test case. The first line has two integer n and k, respectively domino number and the number of opportunities.( 2≤k,n≤1000002\leq k, n \leq 100000 2k,n100000) The second line has n - 1 integers, the distance of adjacent domino d, 1≤d≤1000001 \leq d \leq 100000 1d100000

Output

For each testcase, output of a line, the smallest sum of all dominoes height

Sample Input
1
4 2
2 3 4
Sample Output
9

题意:
给出一列扑克牌的距离,有k次机会,n张扑克牌,问全部推倒扑克牌,扑克牌的最小高度。

分析:
贪心排序,找到前n-k个数相加即可。(详情见bc的题解)

//贪心,将位置距离重排,取前n-k个,奥妙重重
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
int t,k,n,a[100100];LL sum;
int main()
{
    for(scanf("%d",&t);t--;)
    {
        scanf("%d%d",&n,&k);
        for(int i=0;i<n-1;i++) scanf("%d",a+i);
        if(n<=k) printf("%d\n",n);
        else
        {
            sort(a,a+n-1);sum=n;
            for(int i=0;i<n-k;i++) sum+=a[i];
            printf("%I64d\n",sum);
        }
    }
}

abs

Accepts: 136
Submissions: 927
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
Problem Description

Given a number x, ask positive integer y≥2y\geq 2y2, that satisfy the following conditions:

  1. The absolute value of y - x is minimal
  2. To prime factors decomposition of Y, every element factor appears two times exactly.
Input

The first line of input is an integer T ( 1≤T≤501\leq T \leq50 1T50) For each test case,the single line contains, an integer x ( 1≤x≤10181\leq x \leq {10} ^ {18} 1x1018​​)

Output

For each testcase print the absolute value of y - x

Sample Input
5
1112
4290
8716
9957
9095
Sample Output
23
65
67
244
70

题意:
给出一个数,找到距离该数最小的数(满足题目要求)。

分析:
明显要对x开方,然后枚举35000以内的素数,如果能够连除某个素数两次及以上,则重新进入循环,否则输出,记住是对开根号后的x加减,int乘以int要加LL,应对爆int的情况出现

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;

#define LL long long
#define F(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define rep(i,a,b) for (int i=(a),_##i=(b); i<=_##i; i++)
#define rek(i,a,b) for (int i=(a),_##i=(b); i>=_##i; i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(b))

const int P=sqrt(1e9);
int prime[P];
bool is_prm[P+5];

bool check(LL x){
    for(int i=1;i<=prime[0]&&prime[i]*prime[i]<=x;++i)
        if(x%prime[i]==0&&(x/=prime[i])%prime[i]==0)
            return 0;
    return 1;
}
inline LL ab(LL x)
{
    return x<0?-x:x;
}
int t;
LL x;
const int eps=1e-8;
int main()
{
    //欧拉筛
    for(int i=2;i<=P;++i){
        if(!is_prm[i])prime[++prime[0]]=i;
        for(int j=1;j<=prime[0]&&i*prime[j]<=P;++j){
            is_prm[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }

    //freopen("in.txt","r",stdin);
    for(scanf("%d",&t);t--;)
    {
        scanf("%I64d",&x);
        for(int yy1=sqrt(x),yy2=yy1+1;;)
        {
            //printf("%lf\n",sqrt(x));
            //printf("yy1=%I64d y1=%I64d\n",yy1,y1);
            //printf("%I64d\n",yy1);
            if(yy1>1&&(x-(LL)yy1*yy1<(LL)yy2*yy2-x))
            {
                if(!check(yy1)) goto f;else {
                printf("%I64d\n",(x-(LL)yy1*yy1));goto flag;
                }
                f:yy1--;
            }
            ///yy2=(LL)sqrt(y2);//printf("yy2=%I64d y2=%I64d\n",yy2,y2);
            else
            {
                if(!check(yy2)) goto g;else {
                printf("%I64d\n",((LL)yy2*yy2-x));goto flag;
                }
                g:yy2++;
            }
        }
        flag:;
    }
    return 0;
}

 


posted @ 2016-07-31 12:17  遗风忘语  阅读(185)  评论(0编辑  收藏  举报