总在不经意的年生,回首彼岸,纵然发现光景绵长。
Almost All Divisors
We guessed some integer number xx. You are given a list of almost all its divisors. Almost all means that there are all divisors except 11and xx in the list.
Your task is to find the minimum possible integer xx that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number.
You have to answer tt independent queries.
Input
The first line of the input contains one integer tt (1≤t≤251≤t≤25) — the number of queries. Then tt queries follow.
The first line of the query contains one integer nn (1≤n≤3001≤n≤300) — the number of divisors in the list.
The second line of the query contains nn integers d1,d2,…,dnd1,d2,…,dn (2≤di≤1062≤di≤106), where didi is the ii-th divisor of the guessed number. It is guaranteed that all values didi are distinct.
Output
For each query print the answer to it.
If the input data in the query is contradictory and it is impossible to find such number xx that the given list of divisors is the list of almost allits divisors, print -1. Otherwise print the minimum possible xx.
Example
input
Copy
2 8 8 2 12 6 4 24 16 3 1 2
output
Copy
48 4
题意:给你一个不重复list,求x。x满足:x的所有因子(除了1和它本身)都在list里。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define inf 1000000007
using namespace std;
typedef long long ll;
int prime[1000020],isprime[1000020],flag[1000020],ct;
int T,N,X;
ll jg;
int xxs(){
int tt=0,i,j;
isprime[1]=isprime[0]=1;
for(i=2;i<1000020;i++){
if(!isprime[i])
prime[tt++]=i;
for(j=0;j<tt&&prime[j]*i<=1000020;j++){
isprime[i*prime[j]]=1;
if(!i%prime[j])
break;
}
}
}
int fx(ll x){
ll i;
for(i=2;i*i<x;i++){
if(x%i==0){
if(flag[i]==0||flag[x/i]==0)
return 0;
ct+=2;
}
}
if(i*i==x){
if(flag[i]==0)
return 0;
ct++;
}
return 1;
}
int main(){
cin>>T;
xxs();
while(T--){
int mx=-inf,mi=inf;
memset(flag,0,sizeof(flag));
cin>>N;
for(int i=0;i<N;i++){
cin>>X;
mx=max(X,mx);
mi=min(X,mi);
flag[X]=1;
}
if(N==1&&!isprime[X]){
cout<<(ll)X*(ll)X<<endl;
continue;
}
jg=(ll)mx*(ll)mi;
ct=0;
if(fx(jg)&&ct==N)
cout<<jg<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
Good String
output
standard output
Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, stringand xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.
You are given a string ss, you have to delete minimum number of characters from this string so that it becomes good.
Input
The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of characters in ss.
The second line contains the string ss, consisting of exactly nn lowercase Latin letters.
Output
In the first line, print one integer kk (0≤k≤n0≤k≤n) — the minimum number of characters you have to delete from ss to make it good.
In the second line, print the resulting string ss. If it is empty, you may leave the second line blank, or not print it at all.
Examples
input
Copy
4 good
output
Copy
0 good
input
Copy
4 aabc
output
Copy
2 ab
input
Copy
3 aaa
output
Copy
3
题意: 给出长度为n的字符串,删除字符,使得字符串中第一个字符和第二个字符不同,第三个和第四个不同......第n-1和第n个字符不同,其中最后剩余的字符串长度一定为偶数。求删除的最少次数和最后得到的字符串。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define inf 1000000007
typedef long long ll;
using namespace std;
char s[205000];
int n,jg;
int main(){
scanf("%d",&n);
scanf("%s",s);
for(int i=n-1;i>0;){
if(s[i]==s[i-1]){
s[i]='#';
i--;
jg++;
}
else
i-=2;
}
if((n-jg)%2==0){
cout<<jg<<endl;
for(int i=0;i<n;i++)
if(s[i]!='#')
cout<<s[i];
}
else{
cout<<jg+1<<endl;
for(int i=1;i<n;i++)
if(s[i]!='#')
cout<<s[i];
}
return 0;
}
Silent Classroom
There are nn students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let xx be the number of such pairs of students in a split. Pairs (a,b)(a,b) and (b,a)(b,a)are the same and counted only once.
For example, if there are 66 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:
- splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4x=4 (33 chatting pairs in the first classroom, 11 chatting pair in the second classroom),
- splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1x=1 (00 chatting pairs in the first classroom, 11 chatting pair in the second classroom).
You are given the list of the nn names. What is the minimum xx we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer nn (1≤n≤1001≤n≤100) — the number of students.
After this nn lines follow.
The ii-th line contains the name of the ii-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 2020. Note that multiple students may share the same name.
Output
The output must consist of a single integer xx — the minimum possible number of chatty pairs.
Examples
input
Copy
4 jorge jose oscar jerry
output
Copy
1
input
Copy
7 kambei gorobei shichiroji kyuzo heihachi katsushiro kikuchiyo
output
Copy
2
input
Copy
5 mike mike mike mike mike
output
Copy
4
Note
In the first sample the minimum number of pairs is 11. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 22. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 44. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
类似于囚犯的问题。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define inf 1000000007
typedef long long ll;
using namespace std;
int n,fg[110],jg;
string s;
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
cin>>s;
if(s[0]>='a'&&s[0]<='z'){
fg[s[0]-'a']++;
// cout<<s[0]-'a'<<' ';
}
else if(s[0]>='A'&&s[0]<='Z'){
fg[s[0]-'A']++;
// cout<<s[0]-'A'<<' ';
}
}
// cout<<endl;
for(int i=0;i<26;i++){
int ls1,ls2;
ls1=fg[i]/2;
ls2=fg[i]-ls1;
ls1--;
ls2--;
if(ls1>0)
jg+=ls1*(ls1+1)/2;
if(ls2>0)
jg+=ls2*(ls2+1)/2;
}
printf("%d\n",jg);
return 0;
}