BestCoder Round #85

HDU【5776】——sum

|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≤10), 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≤100000, 1≤m≤5000).
2.The second line contains n positive integers x (1≤x≤100) 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的倍数。搞一下序列模m的前缀和,判断是不是存在相同的值。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

const double PI = acos(-1.0);

const double e = exp(1);

typedef long long LL;

int T;

int n,m;

int a[111111];

int vis[5500];

int main()
{
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d %d",&n,&m);

        a[0] =0;

        memset(vis,0,sizeof(vis));

        for(int i  = 1;i<=n;i++)
        {
            scanf("%d",&a[i]);

            a[i] = (a[i-1]+a[i])%m;
        }

        bool flag = false;

        vis[0] = true;

        for(int i = 1;i<=n;i++)
        {
            if(vis[a[i]])
            {
                flag = true;

                break;
            }

            vis[a[i]] = true;
        }

        if(flag) printf("YES\n");

        else printf("NO\n");
    }

    return 0;
}

HDU【5777】——domino

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≤10)
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≤100000)
The second line has n - 1 integers, the distance of adjacent domino d, 1≤d≤100000

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

首先骨牌只要考虑都往右推,其次能带倒骨牌的前提是高度大于等于距离+1。所以如果推一次,那么就是骨牌高度=离下一块骨牌距离+1. 把第一块左边距离设为无穷大,能推nk次,那么就是找nk块左边距离最大的向右推倒即可,所以只需要排序找到前nk-1大的距离

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

const double PI = acos(-1.0);

const double e = exp(1);

typedef long long LL;

int T;

int n,k;

int a[110000];

int main()
{
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d %d",&n,&k);


        LL sum = n;

        for(int i = 0; i<n-1; i++)
        {
            scanf("%d",&a[i]);

            sum+=a[i];
        }
        if(k>=n)
        {
            sum = n;
        }
        else

        {
            sort(a,a+n-1);

            for(int i = n-2; i>=(n-1-k+1); i--)
            {
                sum-=a[i];
            }
        }


        printf("%I64d\n",sum);
    }

    return 0;
}

HDU【5778】——abs

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Problem Description

Given a number x, ask positive integer y≥2, 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≤50)
For each test case,the single line contains, an integer x ( 1≤x≤10 18 )

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,求正整数 y2 ,使得满足以下条件:1.y-x的绝对值最小。2.y的质因数分解式中每个质因数均恰好出现2次。y一定是一个完全平方数,我们对y开方,得到的数中所有的质因子只出现一次,我们枚举这个数就行

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

const double PI = acos(-1.0);

const double e = exp(1);

const int INF = 0x3f3f3f3f;

typedef long long LL;

const int Max = 1e6+100;

bool vis[Max];

LL pr[Max];

void Init()
{
    pr[0] = 0;

    for(int i = 2; i<Max; i++)
    {
        if(!vis[i])
        {
            pr[++pr[0]] = i;

            for(int j = i+i; j<Max; j+=i)
            {
                vis[j] = true;
            }
        }
    }
}

bool ok(LL s)
{
    if(s == 1) return false;

    for(int i = 1; i<=pr[0] && pr[i] * pr[i] <=s ; i++)
    {
        if(s%(pr[i]*pr[i]) ==0) return false;
    }

    return true;
}

LL Check(LL s,LL d)
{
    while(s>0)
    {
        if(ok(s)) return s;

        s+=d;
    }

    return -1;
}

int main()
{
    Init();

    int T;

    LL x;

    cin>>T;

    while(T--)
    {
        cin>>x;

        if(x == 1)
        {
            printf("3\n");

            continue;
        }

        if(x == 2)
        {
            printf("2\n");

            continue;
        }

        LL y = sqrt(x);

        LL a1 = Check(y,-1);

        LL a2 = Check(y+1,1);

        LL ans = min(abs(a1*a1-x),abs(a2*a2-x));

        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(BestCoder Round #85)