您的位置:首页 > 其它

BestCoder Round #85 <同余--贪心--单质数判断+枚举>

2016-07-31 08:51 330 查看


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
\leq T \leq 101≤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
\leq n \leq 1000001≤n≤100000, 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)

问题描述
给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO


输入描述
输入文件的第一行有一个正整数T(1\leq T \leq 101≤T≤10),表示数据组数。

接下去有T组数据,每组数据的第一行有两个正整数n,m (1\leq n\leq 1000001≤n≤100000 ,1\leq m\leq50001≤m≤5000).

第二行有n个正整数x (1\leq x\leq 1001≤x≤100)表示这个数列。


输出描述
输出T行,每行一个YES或NO。


输入样例
2
3 3
1 2 3
5 7
6 6 6 6 6


输出样例
YES
NO


可以算是同余定理吧--只要有余数重复出现就说明从上一个此余数后一个到这个的数之和是m的倍数

代码:

#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)

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
\leq T \leq 101≤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\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 of 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)

问题描述
小白在玩一个游戏。桌子上有n张多米诺骨牌排成一列。它有k次机会,每次可以选一个还没有倒的骨牌,向左或者向右推倒。每个骨
牌倒下的时候,若碰到了未倒下的骨牌,可以把它推倒。小白现在可以随意设置骨牌的高度,但是骨牌高度为整数,且至少为1,并且
小白希望在能够推倒所有骨牌的前提下,使所有骨牌高度的和最小。

输入描述
第一行输入一个整数T(1\leq T \leq 101≤T≤10)
每组数据有两行
第一行有两个整数n和k,分别表示骨牌张数和机会次数。(2\leq k,n\leq 1000002≤k,n≤100000)
第二行有n-1个整数,分别表示相邻骨牌的距离d,1\leq d \leq 1000001≤d≤100000

输出描述
对于每组数据,输出一行,最小的高度和

输入样例
1
4 2
2 3 4

输出样例
9


思路:

如果有k次机会--那么就可以少定义k-1个距离---根据贪心思维当然是k-1个大的数了---

所以就是距离总和减去前k-1个大的距离(注意:最多减(n-1)个)然后加上n---

代码:

#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)

Problem Description

Given a number x, ask positive integer y\geq
2y≥2,
that satisfy the following conditions:
The absolute value of y - x is minimal
To prime factors decomposition of Y, every element factor appears two times exactly.

Input

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

Output

For each testcase print the absolute value of y - x

Sample Input

Copy
5
1112
4290
8716
9957
9095


Sample Output

23
65
67
244
70



abs

 
 Accepts: 136
 
 Submissions: 927

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

问题描述
给定一个数x,求正整数y\geq 2y≥2,使得满足以下条件:
1.y-x的绝对值最小
2.y的质因数分解式中每个质因数均恰好出现2次。

输入描述
第一行输入一个整数T(1\leq T\leq 501≤T≤50)
每组数据有一行,一个整数x(1\leq x\leq {10}^{18}1≤x≤10​18​​)

输出描述
对于每组数据,输出一行y-x的最小绝对值

输入样例
5
1112
4290
8716
9957
9095

输出样例
23
65
67
244
70


先对x开根号--

然后检验两边的值是不是单质数之和---

检验时记得小于2的都要直接return  false;

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL __int64
#define lld I64d
int find(LL xx)
{
if (xx<2) return false;
LL i;
for (i=2;i*i<=xx;i++)
{
if (xx%i==0)
{
xx/=i;
if (xx%i==0)
return false;
}
}
return true;
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
LL n,sq;
scanf("%lld",&n);
sq=sqrt(n);
if (sq*sq==n&&find(sq))
{
printf("0\n");
continue;
}
if (sq*sq==n)//整除先检验左边,再检验右边
{
LL xx=0,yy=0;
for (LL i=1;i;i++)
{
if (find(sq-i))
{
printf("%lld\n",abs(n-(sq-i)*(sq-i)));
break;
}
if (find(sq+i))
{
printf("%lld\n",abs(n-(sq+i)*(sq+i)));
break;
}
}
}
else//不整除时,根号和它+1在同一优先级==然后向两边检验
{
LL xx=0,yy=0;
for (int i=0;;i++)
{
if (find(sq-i))
{
xx=abs(n-(sq-i)*(sq-i));
}
if (find(sq+i+1))
{
yy=abs(n-(sq+i+1)*(sq+i+1));
}
if (xx&&yy)
{
printf("%lld\n",min(xx,yy));
break;
}
if (xx||yy)
{
printf("%lld\n",max(xx,yy));
break;
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: