题解 | #Aquarium Arrangement#
Aquarium Arrangement
https://ac.nowcoder.com/acm/contest/18454/A
I.Jigsaw
思路:首先要可以拼图,则角块c==4。其次,边缘块e不能为奇数。
满足以上条件时:中心块m==0,一定可以;m!=0时,当x1+x2=e/2,x1*x2=m;
暴力枚举x1,x2 看是否满足条件。
代码如下
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } const int mod=1e9+7; const int N=1e5+7; int c,e,m,i; bool solve(){ cin>>c>>e>>m; if(c!=4) return false; if(e&2!=0) return false; if(m==0) {cout<<e/2+2<<" "<<"2"<<endl;return true;} else{ int x=e/2; for(i=1;i<x;i++){ if(i*(x-i)==m){ cout<<max(i+2,x-i+2)<<" "<<min(i+2,x-i+2)<<endl; return true; } } } return false; } int main(){ if(!solve()) cout<<"impossible"<<endl; }
然后 我们发现 x1+x2=e/2 x1*x2=m,由韦达定理 我们可以解方程 x^2-(e/2)x+m=0;
这样写程序更快
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll c,e,m; cin>>c>>e>>m; if(c!=4||e%2==1){ puts("impossible"); return 0; } else{ e/=2; ll t=e*e-4*m; if(t<0){ puts("impossible"); } else{ ll x1=e+sqrt(t); ll x2=e-sqrt(t); //cout<<x1<<" "<<x2<<endl; if(x1+x2!=2*e||x1*x2!=4*m){ puts("impossible"); } else cout<<x1/2+2<<" "<<x2/2+2<<endl; } } }