sum
Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. output YES, otherwise output NO
The first line of the input has an integer T (1≤T≤101 \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≤n≤1000001 \leq n \leq 1000001≤n≤100000, 1≤m≤50001 \leq m \leq 50001≤m≤5000). 2.The second line contains n positive integers x (1≤x≤1001 \leq x \leq 1001≤x≤100) according to the sequence.
Output T lines, each line print a YES or NO.
2
3 3
1 2 3
5 7
6 6 6 6 6
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+; bool used[maxn]; int main()
{
int n, m;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
memset(used, , sizeof(used));
used[] = true;
int sum = ,x;
bool flag = false;
if(n>=m)
{
flag=;
for(int i=;i<n;i++) scanf("%d",&x);
}
else
{
for(int i = ; 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 ;
}
/*
2
3 3
3 1 1
3 7
6 6 9
*/
domino
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.
The first line of input is an integer T ( 1≤T≤101 \leq T \leq 10 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≤1000002\leq k, n \leq 100000 2≤k,n≤100000) The second line has n - 1 integers, the distance of adjacent domino d, 1≤d≤1000001 \leq d \leq 100000 1≤d≤100000
For each testcase, output of a line, the smallest sum of all dominoes height
1
4 2
2 3 4
9
题意:
给出一列扑克牌的距离,有k次机会,n张扑克牌,问全部推倒扑克牌,扑克牌的最小高度。
分析:
贪心排序,找到前n-k个数相加即可。(详情见bc的题解)
//贪心,将位置距离重排,取前n-k个,奥妙重重
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
int t,k,n,a[];LL sum;
int main()
{
for(scanf("%d",&t);t--;)
{
scanf("%d%d",&n,&k);
for(int i=;i<n-;i++) scanf("%d",a+i);
if(n<=k) printf("%d\n",n);
else
{
sort(a,a+n-);sum=n;
for(int i=;i<n-k;i++) sum+=a[i];
printf("%I64d\n",sum);
}
}
}
abs
Given a number x, ask positive integer y≥2y\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.
The first line of input is an integer T ( 1≤T≤501\leq T \leq50 1≤T≤50) For each test case,the single line contains, an integer x ( 1≤x≤10181\leq x \leq {10} ^ {18} 1≤x≤1018)
For each testcase print the absolute value of y - x
5
1112
4290
8716
9957
9095
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+]; bool check(LL x){
for(int i=;i<=prime[]&&prime[i]*prime[i]<=x;++i)
if(x%prime[i]==&&(x/=prime[i])%prime[i]==)
return ;
return ;
}
inline LL ab(LL x)
{
return x<?-x:x;
}
int t;
LL x;
const int eps=1e-;
int main()
{
//欧拉筛
for(int i=;i<=P;++i){
if(!is_prm[i])prime[++prime[]]=i;
for(int j=;j<=prime[]&&i*prime[j]<=P;++j){
is_prm[i*prime[j]]=;
if(i%prime[j]==)break;
}
} //freopen("in.txt","r",stdin);
for(scanf("%d",&t);t--;)
{
scanf("%I64d",&x);
for(int yy1=sqrt(x),yy2=yy1+;;)
{
//printf("%lf\n",sqrt(x));
//printf("yy1=%I64d y1=%I64d\n",yy1,y1);
//printf("%I64d\n",yy1);
if(yy1>&&(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 ;
}