军训场E,F

E - 这题题目超短

思路:贪心,到达n的方法有很多种,但因为n的范围为1e9,所以我们不能暴力枚举,我们贪心的想,因为我们走三步肯定是比走两步能够更快的到达n,所以我们尽可能的去先走三步,当然肯定会出现走不满的情况,这种时候我们用2步的方法进行补全即可。

#include

using namespace std;
const int N=4e6+5;
typedef long long ll;
typedef pair<ll,ll > pll;
typedef array<int,3> p3;
int mod=998244353;
const int maxv=4e6+5;

void solve()
{	
	int n;
	cin>>n;
	int x=abs(n)%3;
	int ans=abs(n)/3;
	if(abs(n)==1){//对于绝对值为1的情况进行特判
		cout<<2<<endl;
		return ;
	}
	if(x==0){
		cout<<ans<<endl;
	}
	else{//当余数为1或者2时,我们都需要再走一步:
	//余数为1时,我们减去最后走的一步3,目前和n相距4,所以此时用两个2补全,余数为2的话直接走一步2即可。
		cout<<ans+1<<endl;
	}

}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int t;
	t=1;
	cin>>t;
    while(t--){
        solve();
    }
	system("pause");
	return 0;
}

F - 这题题目更短

思路:考察对于式子的变形。
对左式进行通分为 v × x + u × y u × v \frac{v \times x+u \times y}{u \times v} u×vv×x+u×y,左式等于右式:
v × x + u × y u × v \frac{v \times x+u \times y}{u\times v} u×vv×x+u×y= x × y u + v \frac{x \times y}{u+v} u+vx×y

⇒ \Rightarrow u × v × ( x + y ) = ( v × x + u × y ) × ( u + v ) u\times v \times (x+y)=(v \times x +u \times y)\times(u+v) u×v×(x+y)=(v×x+u×y)×(u+v)
然后把这个式子展开就行,最终得: v 2 × x + u 2 × y = 0 v^2\times x+u^2 \times y=0 v2×x+u2×y=0
x = − u 2 , y = v 2 x=-u^2,y=v^2 x=u2,y=v2即可。因为u,v的范围为1e9,所以记得开long long。

#include

using namespace std;
const int N=4e6+5;
typedef long long ll;
typedef pair<ll,ll > pll;
typedef array<int,3> p3;
int mod=998244353;
const int maxv=4e6+5;

void solve()
{	
	ll u,v;
	cin>>u>>v;
	ll x=u*u,y=-v*v;
	cout<<x<<" "<<y<<endl;

}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int t;
	t=1;
	cin>>t;
    while(t--){
        solve();
    }
	system("pause");
	return 0;
}

你可能感兴趣的:(算法)