UVa 10493 Cats, with or without Hats

題意:一喵可帶一帽,一帽可裝n喵。m只喵沒帶帽子,有且只有1只喵不在其它喵帶的帽子裏。問有幾喵。

思路:不在其它喵帽子裏的那個喵是樹根。由此形成一棵樹,每隻喵要麼無子節點,要麼有n個子節點。顯然總數y爲帽子的裏喵的和加上不在帽子裏的喵。設總數爲y,則有

y = (y - m) * n + 1  =>  y = (n*m - 1) / (n - 1)。

注意n==1時,m可爲任意值,即Multiple。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;

typedef long long LL;
#define mem(a, n) memset(a, n, sizeof(a))
#define ALL(v) v.begin(), v.end()
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pb push_back
#define eps 1e-8
const int inf = 0x3f3f3f3f, N = 1e3 + 5, MOD = 1e9 + 7;

int T, cas = 0;
LL n, m;

int main(){
#ifdef LOCAL
    freopen("/Users/apple/input.txt", "r", stdin);
//	freopen("/Users/apple/out.txt", "w", stdout);
#endif
	
    while(scanf("%lld%lld", &n, &m), n + m) {
    	LL tmp = n * m - 1;
    	if(n == 1) {
    		if(m != 1) printf("%lld %lld Impossible\n", n, m);
    		else printf("%lld %lld Multiple\n", n, m);
    		continue;
    	}
    	if(tmp % (n - 1) == 0) printf("%lld %lld %lld\n", n, m, tmp / (n - 1));
    	else printf("%lld %lld Impossible\n", n, m);
    }
    
    return 0;
}


發佈了115 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章