#include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <map> #include <stdlib.h> #include <vector> #include <cstring> #include <algorithm> using namespace std; int constantt = 1000000007; unsigned long long modFact(unsigned long long n) { unsigned long long p = constantt; //if (n >= p) // return 0; unsigned long long result = 1; for (unsigned long long i = 1; i <= n; i++) result = (result * i) % p; return result; } unsigned long long comp(unsigned long long k, unsigned long long n) { unsigned long long nsil = modFact(n); unsigned long long ksil = modFact(k); unsigned long long nmksil = modFact(n - k); unsigned long long res = (nsil / (ksil * nmksil) ); return res; } unsigned long long gcd(unsigned long long x, unsigned long long y) { while (y != 0) { unsigned long long t = x % y; x = y; y = t; } return x; } unsigned long long choose(unsigned long long n, unsigned long long k) { if (k > n) throw std::invalid_argument("invalid argument in choose"); unsigned long long r = 1; for (unsigned long long d = 1; d <= k; ++d, --n) { unsigned long long g = gcd(r, d); r /= g; unsigned long long t = n / (d / g); if (r > std::numeric_limits<unsigned long long>::max() / t) throw std::overflow_error("overflow in choose"); r *= t; } return r; } unsigned long long calc2(std::map<int, int> & map, int index, int suma) { int range = suma + 1; std::map<int, int>::iterator iter = map.begin(); std::advance(iter, index); if (iter == map.end()) { return 1; } else { if (iter->first > range) { return 0; } unsigned long long result = 0; if (iter->second == 1) { result++; } for (int i = 1; i <= iter->second; i++) { unsigned long long mnoznik = choose( iter->second, i); //printf("mnoznik: %d dla %d z %d\n", mnoznik, i, iter->second); //printf("Wywoluje suma = %d Obecny : %d a jest go : %d Obecny res: %d\n", suma + i * iter->first, iter->first, iter->second, result); unsigned long long wynik = calc2(map, index + 1, suma + i * iter->first); //printf("result dla %d: obecny result: %d wynik %d * %d Dylo ich: %d\n",iter->first, result, wynik, mnoznik, i); result += mnoznik * wynik; result %= constantt; } return result % constantt; } return 1; } unsigned long long calc(std::map<int, int> & map) { int ileJedynek = map[1]; unsigned long long result = 0; if (ileJedynek == 1) { result++; } for (int i = 1; i <= ileJedynek; i++) { unsigned long long mnoznik = choose( ileJedynek, i); //printf("mnoznik: %d dla %d z %d\n", mnoznik, i, ileJedynek); //printf("Wywoluje dla jedynek suma = %d\n", i); result += mnoznik * calc2(map, 1, i); result %= constantt; } return result % constantt; } int main() { int edges = 15; scanf("%d", &edges); vector<int> map; for (int i = 0 ; i < edges; i++) { int tmp; scanf("%d", &tmp); map.push_back(tmp); } std::map<int, int> mapp; for (int i = 0 ; i < map.size(); i++) { mapp[map[i]]++; } if (mapp.count(1) == 0) { printf("0"); } else { unsigned long long res = calc(mapp); res %= constantt; printf("%llu", res); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <map> #include <stdlib.h> #include <vector> #include <cstring> #include <algorithm> using namespace std; int constantt = 1000000007; unsigned long long modFact(unsigned long long n) { unsigned long long p = constantt; //if (n >= p) // return 0; unsigned long long result = 1; for (unsigned long long i = 1; i <= n; i++) result = (result * i) % p; return result; } unsigned long long comp(unsigned long long k, unsigned long long n) { unsigned long long nsil = modFact(n); unsigned long long ksil = modFact(k); unsigned long long nmksil = modFact(n - k); unsigned long long res = (nsil / (ksil * nmksil) ); return res; } unsigned long long gcd(unsigned long long x, unsigned long long y) { while (y != 0) { unsigned long long t = x % y; x = y; y = t; } return x; } unsigned long long choose(unsigned long long n, unsigned long long k) { if (k > n) throw std::invalid_argument("invalid argument in choose"); unsigned long long r = 1; for (unsigned long long d = 1; d <= k; ++d, --n) { unsigned long long g = gcd(r, d); r /= g; unsigned long long t = n / (d / g); if (r > std::numeric_limits<unsigned long long>::max() / t) throw std::overflow_error("overflow in choose"); r *= t; } return r; } unsigned long long calc2(std::map<int, int> & map, int index, int suma) { int range = suma + 1; std::map<int, int>::iterator iter = map.begin(); std::advance(iter, index); if (iter == map.end()) { return 1; } else { if (iter->first > range) { return 0; } unsigned long long result = 0; if (iter->second == 1) { result++; } for (int i = 1; i <= iter->second; i++) { unsigned long long mnoznik = choose( iter->second, i); //printf("mnoznik: %d dla %d z %d\n", mnoznik, i, iter->second); //printf("Wywoluje suma = %d Obecny : %d a jest go : %d Obecny res: %d\n", suma + i * iter->first, iter->first, iter->second, result); unsigned long long wynik = calc2(map, index + 1, suma + i * iter->first); //printf("result dla %d: obecny result: %d wynik %d * %d Dylo ich: %d\n",iter->first, result, wynik, mnoznik, i); result += mnoznik * wynik; result %= constantt; } return result % constantt; } return 1; } unsigned long long calc(std::map<int, int> & map) { int ileJedynek = map[1]; unsigned long long result = 0; if (ileJedynek == 1) { result++; } for (int i = 1; i <= ileJedynek; i++) { unsigned long long mnoznik = choose( ileJedynek, i); //printf("mnoznik: %d dla %d z %d\n", mnoznik, i, ileJedynek); //printf("Wywoluje dla jedynek suma = %d\n", i); result += mnoznik * calc2(map, 1, i); result %= constantt; } return result % constantt; } int main() { int edges = 15; scanf("%d", &edges); vector<int> map; for (int i = 0 ; i < edges; i++) { int tmp; scanf("%d", &tmp); map.push_back(tmp); } std::map<int, int> mapp; for (int i = 0 ; i < map.size(); i++) { mapp[map[i]]++; } if (mapp.count(1) == 0) { printf("0"); } else { unsigned long long res = calc(mapp); res %= constantt; printf("%llu", res); } return 0; } |