WHALEon120.github.io

TIOJ 1015. A.Squares in Rectangle

回前頁

題目敘述

點我

我的想法

用數學歸納法推出公式
在輸出(廢話

我的AC CODE

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
    ll n,m;
    while(cin>>n>>m&&n+m!=0){
        if(n>m) swap(n,m);
        ll r = n*(n+1)*(3*m-n+1)/6;
        cout<<r<<'\n';
    }
}

感想

一開始使用for一直加,然後就TLE了QQ

我的TLE CODE

#include <bits/stdc++.h>
using namespace std;

long long min(long long a, long long int b){
	if(a>b){
	return b;
	}
	else{
		return a;
	}
}
int main() {
	// your code goes here
	ios::sync_with_stdio(0);
	cin.tie(0);
	long long a, b, r;
	while(cin >> a >> b){
		r=0;
		if(a !=0 and b != 0){
		for(long long i=0;i<min(a,b);i++){
			r += (a-i)*(b-i);
		}
		cout << r <<'\n';
		}
	
	else{
		break;
	}
	}
	return 0;
}

給大家笑一笑
後來上網查才發現公式,改了CODE 並且使用swap函數
數學還是很重要XDD
註:但我npsc就是太相信數學XDD