Problem:
Problem B:
Square Coins
People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (= 17
2
), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland.
There are four combinations of coins to pay ten credits:
ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Silverland.
Input
The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.
Output
For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output.
Sample Input
2
10
30
0
Output for the Sample Input
1
4
27

Submitted solutions:

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

int count( int S[], int m, int n )
{
    if (n == 0)
        return 1;
    if (n < 0)
        return 0;
    if (m <=0 && n >= 1)
        return 0;
    return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}
int main()
{
    int n;
    while(cin>>n){
     if(n==0) exit(0);
     else{
    int arr[300];
     int j=0;
     for(int i=1;i<=n;i++){
        if((i*i)<=n){
            arr[j++]=i*i;
        }
     }
    cout<<count(arr,j,n)<<endl;
    }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3020
final_rank: 9

code_snippet: #include <iostream>
#include <string>
#include<bits/stdc++.h>
using namespace std;
 
int coin[17];
int ans;
 
void solve(int n, int u){
 
    for (int i = u; i >= 0; i--){
        if (n > coin[i]) solve(n - coin[i], i);
        else if (n == coin[i]) ans++;
    }
 
    return;
}
 
 
int main(void){
 
    int n;

    for (int i = 0; i < 17; i++){
        coin[i] = (i + 1) * (i + 1);
    }
 
    while (cin >> n, n){
        ans = 0;
        solve(n, 16);
        cout << ans << endl;
    }
 
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1168
final_rank: 5

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


int square[] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289 };
int ans;


void dfs(int num, int upper){
	//cout << num << "\t" << upper << endl;

	for (int i = upper; i >= 0; i--){
		if (num > square[i]) dfs(num - square[i], i);
		else if (num == square[i]) ans++;
	}

	return;
}


int main(void){

	int num;

	while (cin >> num, num){
		ans = 0;
		dfs(num, 16);

		cout << ans << endl;

	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1164
final_rank: 2

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;

vector<vector<ll>> dp(1000,vector<ll>(20,0));

vary(int,v,0,0);
int tar;
int DP(int i,int k){
  if(k == tar){
    return 1;
  }
  if(k > tar) return 0;
  int cnt = 0;
  dp[i][k] = 1;
  REP(j,i,18){
    cnt += DP(j,k+v[j]);
  }
  return cnt;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  v.clear();
  REP(i,0,18){
    v.pb(i*i);
  }
  int n;
  while(cin >> n&&n){
    tar = n;
    dp = vector<vector<ll>>(1000,vector<ll>(1000,0));
    cout << DP(1,0) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 18652
final_rank: 16

code_snippet: #include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
using namespace std;
typedef pair<int ,int > P;
typedef long long ll;


ll dfs(int x,int a) {
    if(x==0) return 1;
    ll ret=0;
    rep(i,18) if(a<=i) {
        if(x-i*i>=0) {
            ret+=dfs(x-i*i,i);
        }
    }

    return ret;
}

int main() {
    ll x;
    while(cin>>x) {
        if(x==0) break;
        cout<<dfs(x,1)<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 1164
final_rank: 13

code_snippet: #ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

int data[18] = {};

long long int saiki(int N,int reb)
{
	if (!N)return 1;
	while (data[reb] > N)reb--;
	long long int count = 0;
	for (size_t i = reb; i >0; i--)
	{
		count += saiki(N - data[i], i);
	}
	return count;
}

int main()
{
	int N;
	for (size_t i = 0; i < 18; i++)
	{
		data[i] = i*i;
	}
	while (cin >> N, N)
	{
		cout << saiki(N, 17) << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1164
final_rank: 2

code_snippet: #include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int n;
int v[17];
int dfs(int pos,int sum){
    if(pos==17){
        if(sum==n)return 1;
        else return 0;
    }
    int res=0;
    for(int i=0;sum+v[pos]*i<=n;i++)res+=dfs(pos+1,sum+v[pos]*i);
    return res;
}
int main(){
    for(int i=0;i<17;i++)v[i]=(i+1)*(i+1);
    while(cin>>n,n)cout<<dfs(0,0)<<endl;
}
accuracy: 1
time_ms: 110
memory_kb: 1160
final_rank: 12

code_snippet: #include <iostream>

using namespace std;

int div[17];

int dfs(int num, int s)
{
    int ret = 0;

    if (num == 0)
        return 1;
    else if (num < 0)
        return 0;

    for (int i = s; i < 17; i++) {
        if (num < div[i])
            break;
        ret += dfs(num - div[i], i);
    }
    return ret;
}

int main()
{
    int n;

    for (int i = 1; i <= 17; i++)
        div[i - 1] = i * i;

    while (cin >> n, n) {
        int ans = 0;

        cout << dfs(n, 0) << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1164
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

void MakeData(int, int, bool, vector<int>, vector<int>&);
void NextData(vector<int>, vector<int>&);

int main(){
  int i, n;
  vector<int> coin, data;

  for(i=1; i<=17; ++i) coin.push_back(i*i);

  while(1){
    cin >> n;
    if(n == 0) break;
    MakeData(n, n, true, coin, data);

    for(i=1; data[0]!=1; ++i) NextData(coin, data);
    cout << i << endl;
    data.clear();
  }
  return 0;
}

void MakeData(int n, int m, bool is_upper,
	      vector<int> coin, vector<int>& data){
  vector<int>::iterator i;
  if(is_upper) i = upper_bound(coin.begin(), coin.end(), m);
  else i = lower_bound(coin.begin(), coin.end(), m);
  --i;
  while(1){
    if(n >= *i){
      data.push_back(*i);
      n -= *i;
      if(n == 0) return;
    }else{
      --i;
    }
  }
}

void NextData(vector<int> coin, vector<int>& data){
  int n, sum;
  vector<int>::iterator i, j;
  i = find(data.begin(), data.end(), 1);
  n = *(i-1);
  if(i == data.end()){
    data.pop_back();
    MakeData(n, n, false, coin, data);
  }else{
    for(j=i-1, sum=0; j!=data.end(); ++j) sum += *j;
    data.erase(i-1, data.end());
    MakeData(sum, n, false, coin, data);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1220
final_rank: 6

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

using namespace std;

static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> paii;

int coin[18] = {0}; 
int n, res = 0;

void solve_rec(int sum, int i)
{
	if( i <= 17)
	{
		if( sum == n)
			res++;
		else if(sum < n)
		{
			solve_rec(sum+coin[i], i);
			solve_rec(sum, i+1);
		}
	}
	
	return;
}

int solve()
{
	res = 0;
	solve_rec(0, 1);
	return res;
}

void init()
{
	FOR(i, 1, 18)
	{
		coin[i] = i*i;
	}
	
	return;
}

int main()
{
	init();
	
	while(cin >> n, n)
	{
		cout << solve() << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 1168
final_rank: 14

code_snippet: #include <iostream>
#include <cstring>

using namespace std;
int dp[18][301][301];
int hoge;
int main(){
    memset(dp,0,sizeof(dp));
    for(int i=0;i<301;++i) dp[0][i][i] = 1;
    
    for(int i=1;i<18;i++){
        for(int j=0;j<301;j++){
            for(int k=0;k<301;++k){
                int cnt=0;
                while((i+1)*(i+1)*cnt+k<301){
                    dp[i][j+cnt][(i+1)*(i+1)*cnt+k] += dp[i-1][j][k];
                    cnt++;
                }
            }
        }
    }

    int n;
    while(cin >> n && n){
        int ret=0;
        for(int i=0;i<301;++i) ret+= dp[17][i][n];
        cout << ret << endl;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 7124
final_rank: 11

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge{int to,from,cost;};

#ifdef DEBUGLOCAL
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;i--)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define repnd(i,m,n) for(int i=(n)-1;i>=(m);i--)
#define rep0(i,n) for(i=0;i<(n);++i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define IL for(;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647/3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,-1,1,-1,0};
const int dy[]={0,0,1,-1,1,-1,-1,1,0};

int solve(int n,int a){
	if(n==0)return 1;
	int res=0;
	for(int i=a;i*i<=n;++i){
		res+=solve(n-i*i,i);
	}
	return res;
}

int main() {
	int n;
	IL{
		cin>>n;
		if(n==0)break;
		cout<<solve(n,1)<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 876
final_rank: 10

code_snippet: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

const int N = 17;
int n;

int dfs(int i)
{
	if (n < 0) {
		return 0;
	}
	if (n == 0) {
		return 1;
	}
	int ans = 0;
	for (int j = i; j <= N; j++) {
		n -= j * j;
		ans += dfs(j);
		n += j * j;
	}
	return ans;
}

void solve()
{
	int ans = dfs(1);
	cout << ans << endl;
}

int main()
{
	while (cin >> n, n) {
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 872
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int n;

int solve(int n,int m){
	if(!n)return 1;
	if(n<0)return 0;
	int res=0;
	rep(i,m)res+=solve(n-(i+1)*(i+1),i+1);
	return res;
}

int main(){
	while(cin>>n&&n)cout<<solve(n,17)<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <cmath>
using namespace std;

int value[301];

int pow(int x, int n) {
  int ans = 1;
  for(int i = 0; i < n; i++) {
    ans *= x;
  }
  return ans;
}

bool isHeiho(int x) {
  return (double)((int)sqrt(x)) == sqrt(x);
}

void rec(int minI, int x, int maxX) {
  if(x == 0) {
    value[maxX]++;
    return;
  }
  for(int i = minI; i <= 17; i++) {
    int tmp = pow(i, 2);
    if(tmp <= x) rec(i, x-tmp, maxX);
    if(tmp > x) break;
  }
}

int main() {
  for(int i = 1; i <= 30; i++) {
    value[i] = 0;
  }

  while(1) {
    int price;
    cin >> price;
    if(price == 0) break;
    if(value[price] == 0) {
      rec(1, price, price);
    }
    cout << value[price] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 868
final_rank: 8

code_snippet: #include<stdio.h>
#include<string.h>

int c,sq[18];

int count(int n,int i){
	if(n==0)return 0;
	int j;
	for(j=i;j>=1;j--){
		if(n<sq[j])continue;
		if(n==sq[j]){
			++c;
			continue;
		}
		count(n-sq[j],j);
	}
	return 0;
}

int main(){

	int i,j,n;
	
	for(i=0;i<=17;i++){
		sq[i]=i*i;
	}
	
	while(0<=scanf("%d",&n)){
		if(n==0)break;
		c=0;
		
		for(i=1;i<=17;i++){
			if(n<sq[i]){
				--i;
				break;
			}
		}
		count(n,i);
		printf("%d\n",c);
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 760
final_rank: 7