Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created June 3, 2010 14:34
Show Gist options
  • Save yaotti/423956 to your computer and use it in GitHub Desktop.
Save yaotti/423956 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
// calculate fib(n+2)-1
int fib_total(long long int val1, long long int val2, long long int n) {
if (n < 1) return val2 - 1;
return fib_total(val2, val1+val2, n-1);
}
void solve(long long int n, int m, int c) {
int total = fib_total(1, 1, n) % m;
if (total == 0 || total % 2 == 1) {
cout << "Case " << c << ": No" << endl;
}else {
cout << "Case " << c << ": Yes" << endl;
int pls = total / 2;
for (int i = 0; i < total; i++) {
string first, second;
first = i < pls ? "1" : "0";
second = i < pls ? "0" : "-1";
for (int j = 0; j < total - i; j++) {
cout << first;
if (j != total-1) cout << " ";
}
for (int j = 0; j < i; j++) {
cout << second;
if (j != i-1) cout << " ";
}
cout << endl;
}
}
}
int main(int argc, char **argv)
{
int total = 0;
cin >> total;
long long int n;
int m;
for (int i = 0; i < total; i++) {
cin >> n >> m;
solve(n, m, i+1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment