Bestcoder Round #85 <同余--贪心--单质数判断+枚举> __ The net Race column

Source: Internet
Author: User

sum accepts:640 submissions:1744 time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others) P Roblem Description

Given a sequence, you ' re asked whether there exists a consecutive subsequence whose the sum is divisible by M. output YES, oth Erwise Output NO Input

The ' the ' input has an integer t (1 \leq T \leq 101≤t≤10), which represents the number of test cases. For the all test case, there are two lines:1.the the contains two positive integers n, m (1 \leq N \leq 1 \leq m \leq 50001≤m≤5000). 2.The second line contains n positive integers x (1 \leq x \leq 1001≤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


sum accepts:640 submissions:1744 time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others) Q Title Description

Given a sequence, whether there is a multiple of continuous columns m, there is output yes, otherwise output no
Input description
The first line of the input file has a positive integer t (1\leq t \leq 101≤t≤10), representing the number of data sets.

Then there is the T-group data, and the first row of each set of data has two positive integers n,m (1\leq n\leq 1000001≤n≤100000, 1\leq m\leq50001≤m≤5000).

The second row has n positive integer x (1\leq X\leq 1001≤x≤100) that represents the sequence.
Output description
Output T row, one yes or no per line.
Input sample
2
3 3 1 2 3 5 7 6 6 6 6-
6
Output sample
YES
NO

It's a congruence theorem--as long as there's more than a few repetitions, which means that the sum of the numbers from the last one to this is a multiple of M.


Code:


#include <cstdio>
#include <cstring>
bool fafe[6000];
int main ()
{
    int t;scanf ("%d", &t);
    while (t--)
    {
        int n,m,a;
        scanf ("%d%d", &n,&m);
        memset (fafe,false,sizeof (Fafe));
        BOOL Fa=false;
        Fafe[0]=true;
        int s=0;
        while (n--)
        {
            scanf ("%d", &a);
            S= (S+a)%m;
            if (Fafe[s])
            fa=true;
            fafe[s]=true;
        }
        if (FA)
        printf ("yes\n");
        else
        printf ("no\n");
    }
    return 0;
}



Domino accepts:462 submissions:1498 time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others) P Roblem 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 in most k times and let it fall to the "left" or right. When a domino is toppled, it'll knock down the erect domino. On the assumption so all of the tiles are fallen in the "end", he can set the height of all dominoes, but him wants to mini Mize the sum of all dominoes height. The height of every domino is a integer and at least 1. Input

The "a" of the input is a integer t (1 \leq T \leq 101≤t≤10) There are two lines of each test case. The has two integer n and K, respectively Domino number and the number of opportunities. (2\leq k, n \leq 1000002≤k,n≤100000) The second line has n-1 integers, the distance of adjacent Domino D, 1 \leq d \leq 1000001≤d≤100000 Output

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

1
4 2
2 3 4
Sample Output
9

Domino accepts:462 submissions:1498 time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others) Q Title Description

Xiao Bai is playing a game. There are n dominoes lined up on the table. It has a K-chance, and each time you can choose a domino that has not been inverted, tear it to the left or right. When each bone
tag falls, it can be pushed down if it encounters a domino that is not down. Small white is now free to set the height of the dominoes, but the domino height is an integer and at least 1, and White wants to make all the dominoes as
high and small as possible with all the dominoes down.
Input description
The first line enters an integer t (1\leq t \leq 101≤t≤10)
There are two rows in each set
of data, the first row has two integers n and K, respectively, representing the number of dominoes and the number of opportunities. (2\leq k,n\leq 1000002≤k,n≤100000)
The second line has a n-1 integer representing the distance of the adjacent Domino D,1\leq D \leq 1000001≤d≤100000
Output description
For each set of data, output one row, the smallest height and
Input sample
1
4 2
2 3 4
Output sample
9

Ideas:

If you have a K-chance-then you can define the k-1 distance less---according to greedy thinking of course is k-1 a large number---

So it's the distance sum minus the front k-1 a big distance (note: Max Minus (n-1)) then add n---


Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
__int64 shu[100100],s;
BOOL cmp (int xx,int yy)
{return
    xx>yy;
}
int main ()
{
    int t,n,k;scanf ("%d", &t);
    while (t--)
    {
        s=0;
        int n;scanf ("%d%d", &n,&k);
        for (int i=0;i<n-1;i++)
        {
            scanf ("%d", &shu[i]);
            S+=shu[i];
        }
        Sort (shu,shu+n-1,cmp);
        for (int i=0;i<k-1&&i<n-1;i++)
        s-=shu[i];
        printf ("%i64d\n", s+n);
    }
    return 0;
}



ABS accepts:136 submissions:927 time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others) Pr Oblem Description

Given A number x, ask positive integer Y\GEQ 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.