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
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int N = ;
int sum[N];
bool vis[];
int main()
{
int tcase,n,m,x;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&m);
sum[] = ;
bool flag = false;
memset(vis,false,sizeof(vis));
for(int i = ;i<=n;i++){
scanf("%d",&x);
sum[i] = sum[i-]+x;
if(sum[i]%m==) flag = true;
if(vis[sum[i]%m]) flag = true;
vis[sum[i]%m] = true;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
int a[N];
int cmp(int a,int b){
return a>b;
}
int main(){
int tcase,n,k;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&k);
long long sum = ;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
sum=sum+a[i]+;
}
if(n<=k){
printf("%d\n",n);
continue;
}
sort(a+,a+n,cmp);
for(int i=;i<k;i++){
sum-=a[i];
}
printf("%I64d\n",sum+);
}
return ;
}
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 题解:分别往左找往右找,如果这个数分解的质因数个数都只有1,那么他本身的平方每个质因数个数就为2了,往右的时候(k+i)*(k+i)>=n这个条件一定要记得写。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const LL INF = (LL)(((LL))<<-);
bool can(LL num)
{
for(int i=; i*i<=num; i++)
{
int cnt = ;
if(num%i==)
{
while(num%i==)
{
num/=i;
cnt++;
if(cnt>) return false;
}
}
}
return true;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{ LL n;
scanf("%I64d",&n);
if(n<=){
printf("%I64d\n",-n);
continue;
}
LL k = sqrt(n);
LL ans = INF;
for(int i=;; i++)
{
if((k+i)*(k+i)>=n&&can(k+i))
{
ans = min(ans,abs((k+i)*(k+i)-n));
break;
}
}
for(int i=;; i++)
{
if(can(k-i)&&(k-i)>=)
{
ans = min(ans,abs((k-i)*(k-i)-n));
break;
}
}
printf("%I64d\n",ans);
}
return ;
}