HDU 5018 Revenge of Fibonacci
题意:给定a, b, c,其中a, b作为定义的斐波那契数列的前两项,求问c是否在这样的数列里面出现。
由于a,b,c都不超过10^9,都是正数,而斐波那契数增长是很快的,所以直接暴力求解即可。
#include<cstdio>
typedef long long LL;
LL a, b, c;
bool ok(){
if(c==a || c==b) return 1;
if(c<a || c<b) return 0;
LL d = a+b;
while(d<c){
a=b; b=d;
d = a+b;
}
return d==c;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%I64d %I64d %I64d", &a, &b, &c);
puts(ok()?"Yes":"No");
}
return 0;
}
HDU 5019 Revenge of GCD
题意:就是给定x和y,求x和y的第k大的公因子。
既然是x和y的公因子,那么也肯定是gcd(x,y)的因子。
所以先求出x和y的最大公约数,再对这个公约数进行因数分解,然后就能输出答案了。
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int T;
LL gcd(LL a, LL b){
if(a<b) swap(a, b);
LL c = a%b;
while(c>0){
a = b; b = c; c=a%b;
}
return b;
}
vector<LL> V;
int main(){
LL a, b, k;
scanf("%d", &T);
while(T--){
scanf("%I64d %I64d %I64d", &a, &b, &k);
a = gcd(a, b);
LL m = (LL)sqrt(a+0.1);
V.clear();
for(LL i=1; i<=m; i++){
if(a%i==0){
V.push_back(i);
if(i*i!=a) V.push_back(a/i);
}
}
sort(V.begin(), V.end());
if(k>V.size()) puts("-1");
else printf("%I64d\n", V[V.size()-k]);
}
return 0;
}
HDU 5020 Revenge of Collinearity
题意:给定平面N个点,求出有多少个集合,集合内必须是3个不同的点,且三个点共线。
貌似这题有不少做法,而且据说姿势不好很容易WA。
难道比赛当时我采取的姿势还挺不错的?
呃咳,我是直接枚举每个点,用map保存其它点跟它的斜率,求个数。注意斜率不存在的要另算即可。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
map<double, int> MP;
const int N = 1010;
int x[N], y[N];
int T, n;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d %d", x+i, y+i);
int ans = 0, cnt;
for(int i=0; i<n; i++){
MP.clear();
cnt = 0;
for(int j=i+1; j<n; j++){
if(x[j]==x[i]){
ans += cnt;
cnt++;
continue;
}
double k = (y[i]-y[j])*1.0/(x[i]-x[j]);
ans += MP[k];
MP[k]++;
}
}
printf("%d\n", ans);
}
return 0;
}
HDU 5021 Revenge of kNN II
题意:每个点有个v值,对于每个查询q,k,求出第q个点的最近的k个邻居(不包括q),把它们的v值加起来求平均数res,作为q点的新值。题目要做的是把所有res加起来。
首先对于点,题目没有保证坐标的大小关系,所以先要按照坐标排个序,建立好点的编号跟它的新编号的映射。
对于询问q, k,求出q的实际位置x,假设我们求出来的k个邻居是l~r,中间包含了x,那么显然这里可以用树状数组解决求和,然后单点更新。
关键就是怎么确定l和r。
我是对x左边取的个数做二分,由于是K个邻居,所以如果左边确定,两边的边界就都能确定。
如果我们发现在右边界的右边有比左边界更优的,说明当前的左边界应该往右移动,否则说明它至少可以保持现状。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100100;
int T, n, m;
struct Point{
int x, v, id;
bool operator < (const Point &A)const{
return x<A.x;
}
}p[N];
inline int lowbit(int &x){
return x&(-x);
}
double s[N], a[N];
void add(int x, double v){
for(; x<=n; x+=lowbit(x)) s[x]+=v;
}
double sum(int x){
double res = 0.0;
for(; x; x-=lowbit(x)) res += s[x];
return res;
}
int to[N];
int judge(int d, int left, int k){
int dl1 = p[d].x - p[d-left].x;
if(d+k-left<n-1){
int dr2 = p[d+k-left+1].x - p[d].x;
if(dr2 < dl1 || (dr2==dl1 && p[d+k-left+1].id<p[d-left].id)) return 1;
}
return 0;
}
int main(){
scanf("%d", &T);
while(T--){
scanf("%d %d", &n, &m);
for(int i=0; i<n; i++){
scanf("%d %d", &p[i].x, &p[i].v);
p[i].id = i+1;
s[i+1] = 0.0;
}
sort(p, p+n);
for(int i=0; i<n; i++){
to[p[i].id] = i;
a[i] = p[i].v*1.0;
add(i+1, a[i]);
}
double ans=0.0, res;
int q, k, low, top, mid;
while(m--){
scanf("%d %d", &q, &k);
int x = to[q];
if(k==n-1){
res = (sum(n) - a[x])/k;
ans += res;
add(x+1, res-a[x]);
a[x] = res;
continue;
}
low = max(0, k-n+x+1);
top = min(x, k);
int sel = low;
while(low<=top){
mid = (low+top)>>1;
if(judge(x, mid, k)){
top = mid-1;
}
else{
low = mid+1;
sel = max(sel, mid);
}
}
low = x - sel;
top = x + k - sel;
res = (sum(top+1) - sum(low) - a[x])/k;
ans += res;
add(x+1, res-a[x]);
a[x] = res;
}
printf("%.3lf\n", ans);
}
return 0;
}