D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+5;
bool vis[maxn];
vector<int> e[maxn];
int dist[maxn];
void dfs(int id,int fa){
for(auto ep:e[id]){
if(ep==fa) continue;
dist[ep]=dist[id]+1;
dfs(ep,id);
}
}
int main(){
//freopen("in.txt","r",stdin);
int T,n,m,x,y;
cin>>T;
while(T--){
scanf("%d%d",&n,&m);
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++) e[i].clear();
for(int i=1;i<=m;i++){
scanf("%d",&x);
vis[x]=1;
}
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
int rt=1;
for(;rt<=n;rt++)
if(vis[rt]) break;
dist[rt]=0;
dfs(rt,-1);
int D=0,id=0;
for(int i=1;i<=n;i++)
if(vis[i]&&dist[i]>D){
D=dist[i];
id=i;
}
dist[id]=0;
dfs(id,-1);
D=0;
for(int i=1;i<=n;i++)
if(vis[i]&&dist[i]>D)
D=dist[i];
printf("%d.00
",D/2);
}
return 0;
}
F
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
//freopen("in.txt","r",stdin);
int T,n;
cin>>T;
while(T--){
scanf("%d",&n);
int t=n/2;
if(n%2==0){
for(int i=1;i<=n/2;i++){
if(i!=1) printf(" ");
printf("%d %d",i,t+i);
}
puts("");
} else {
t++;
for(int i=1;i<=n/2;i++){
if(i!=1) printf(" ");
printf("%d %d",i,t+i);
}
printf(" %d
",t);
}
}
return 0;
}
H
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> v;
const int maxn = 1e5+5;
int a[maxn],n;
int MOD[maxn];
int main(){
//freopen("in.txt","r",stdin);
int T;
cin>>T;
while(T--){
ll s=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
s+=a[i];
}
v.clear();
for(ll i=2;i*i<=s;i++){
if(s%i==0){
v.push_back(i);
while(s%i==0)
s/=i;
}
}
if(s!=1) v.push_back(s);
ll ans=100000000000000ll;
for(auto x:v){
ll res=0;
ll tot=0;
for(int i=1;i<=n;i++)
MOD[i]=a[i]%x,tot+=MOD[i];
sort(MOD+1,MOD+1+n);
int id=n;
while(tot!=0&&id>=1){
res+=x-MOD[id--];
tot-=x;
}
if(tot==0&&res<ans)
ans=res;
}
printf("%lld
",ans);
}
return 0;
}
J
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
inline ll power(ll a, ll n, ll p) {
ll ret = 1; ll now = a;
while (n != 0) {
if (n & 1)
ret = ret * now % p;
now = now * now % p;
n >>= 1;
}
return ret;
}
inline ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
int main() {
//freopen("in.txt","r",stdin);
int T, n, d;
cin >> T;
while (T--) {
scanf("%d%d", &n, &d);
ll a, b;
if (d == 1) {
a = (n + 2);
b = 4ll;
} else {
a = 3ll * (n + 2);
b = 8ll;
}
ll g = gcd(a, b);
a /= g; b /= g;
//cout << a << " " << b << endl;
cout << (ll)a*power(b, mod - 2, mod) % mod << endl;
}
return 0;
}
L
hdu 6241
M
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-3;
const int maxn = 1e5 + 5;
int T, n;
struct vec {
double x, y;
vec() {}
vec(double _x, double _y) {x = _x; y = _y;}
};
struct circle {
double x, y;
double r;
};
vec p[maxn];
int a, b, c, cnt;
double d;
double mx = 1e9;
int sgn(double x) {
if (x > eps) return 1;
if (x < -eps) return -1;
return 0;
}
double dist(vec a, vec b) {
double tx = a.x - b.x;
double ty = a.y - b.y;
return sqrt(tx * tx + ty * ty);
}
int rando() {
return (rand() << 15 ) | rand();
}
circle get(vec a, vec b, vec c) {
circle ret;
double A1, A2, B1, B2, C1, C2, tmp;
A1 = a.x - b.x;
B1 = a.y - b.y;
C1 = (a.x * a.x - b.x * b.x + a.y * a.y - b.y * b.y) / 2;
A2 = c.x - b.x;
B2 = c.y - b.y;
C2 = (c.x * c.x - b.x * b.x + c.y * c.y - b.y * b.y) / 2;
tmp = A1 * B2 - A2 * B1;
ret.x = (C1 * B2 - C2 * B1) / tmp;
ret.y = (A1 * C2 - A2 * C1) / tmp;
ret.r = dist(vec(ret.x, ret.y), a);
return ret;
}
bool sameline(vec a, vec b, vec c) {
double x1, y1, x2, y2;
x1 = a.x - b.x;
y1 = a.y - b.y;
x2 = a.x - c.x;
y2 = a.y - c.y;
double Cx = x1 * y2 - x2 * y1;
if (sgn(Cx) == 0) return 1;
return 0;
}
int main() {
freopen("in.txt", "r", stdin);
srand(time(0));
cin >> T;
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
if (n == 1) {
printf("%lf %lf %lf
", p[1].x, p[1].y, 0.0);
} else if (n <= 4) {
printf("%lf %lf %lf
", (p[1].x + p[2].x) / 2.0, (p[1].y + p[2].y) / 2.0, dist(vec((p[1].x + p[2].x) / 2.0, (p[1].y + p[2].y) / 2.0), p[2]));
} else {
circle ans;
for (;;) {
int a = (rando() % n + n) % n + 1;
while ((b = (rando() % n + n) % n + 1) == a);
while ((c = (rando() % n + n) % n + 1) == b || c == a);
if (sameline(p[a], p[b], p[c])) continue;
//cout << a << " " << b << " " << c << endl;
ans = get(p[a], p[b], p[c]);
if (fabs(ans.x) > mx || fabs(ans.y) > mx || ans.r > mx)continue;
cnt = 0;
for (int j = 1; j <= n; j++) {
d = dist(vec(ans.x, ans.y), p[j]);
if (sgn(d - ans.r) == 0)
cnt++;
}
if (cnt >= (n / 2 + (n % 2 == 1))) {
// if (sgn(ans.x) == 0) ans.x = 0.0;
// if (sgn(ans.y) == 0) ans.y = 0.0;
printf("%.6f %.6f %.6f
", ans.x, ans.y, ans.r);
break;
}
}
}
}
return 0;
}