50分的一个搜索写法:
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 | #include<iostream> #include<algorithm> #include<cstdio> using namespace std; int result[205]; int pow2[10]={1,2,4,8,16,32,64,128,256,512}; int k,w; inline void percolate(){ for(int i=1;i<=203&&result[i]>10000;i++){ result[i+1]+=result[i]/10000; result[i]%=10000; } } inline void dfs(int pos,int num){ if(num==0)return; if((w%k==0?w/k:w/k+1)<pos)return; if(w%k!=0&&(w/k+1)==pos)num=min(pow2[w%k]-1,num); for(int i=1;i<=num;i++)dfs(pos+1,i-1); if(pos!=1){ result[1]+=num; percolate(); } } int main(){ cin>>k>>w; dfs(1,pow2[k]-1); int flag=0; for(int i=204;i>0;i--){ if(result[i])flag++; if(result[i]==0){if(flag)cout<<"0000";} else printf((flag==1?"%d":"%04d"),result[i]); } } |
100分写法:
参考题解:https://www.luogu.org/blog/user50852/solution-p1066
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #include<iostream> #include<string> #include<algorithm> #define PINF 0x7fffffff using namespace std; struct bigInt { //For Non-negative Numbers // You must include header **iostream** and **string** before using this class! private: string v; static string & bIsort(string & v) { //each char has reduced '0' for (int i = 0; i < v.length(); i++) { if (v[i] > 9) { if (i == v.length() - 1)v.append(1, v[i] / 10); else v[i + 1] += v[i] / 10; v[i] %= 10; } } return v; } static string lng2str(long long v) { string s = ""; while (v) { s.append(1, v % 10 + '0'); v /= 10; } reverse(s.begin(), s.end()); return s; } static string& trimPre0(string& v3) { while (v3.length() != 0 && (*v3.begin()) == '0')v3.erase(v3.begin()); if (v3.length() == 0)v3 = "0"; return v3; } public: bigInt() { v = "0"; } bigInt(long long n) { v = lng2str(n); } bigInt(string s) { v = s; } bigInt& operator = (const bigInt & bI) { this->v = bI.v; return *this; } bigInt operator + (bigInt bI2) const { string s1 = v, s2 = bI2.v; if (s1.length() > s2.length())swap(s1, s2); reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int s1len = s1.length(), s2len = s2.length(); for (int i = 0; i < s1len; i++)s2[i] += s1[i] - '0'; for (int i = 0; i < s2len - 1; i++)if (s2[i] > '9') { s2[i + 1] += (s2[i] - '0') / 10; s2[i] = '0' + (s2[i] - '0') % 10; } if (s2[s2len - 1] > '9') { s2.append(1, '0' + (s2[s2len - 1] - '0') / 10); s2[s2len - 1] = '0' + (s2[s2len - 1] - '0') % 10; } reverse(s2.begin(),s2.end()); return bigInt(s2); } bigInt operator - (bigInt bI2) const { string s1, s2; if ((*this) < bI2) { s1 = v; s2 = bI2.v; } else { s1 = bI2.v; s2 = v; } reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int s1len = s1.length(), s2len = s2.length(); for (int i = 0; i<s1len; i++)s2[i] -= s1[i] - '0'; for (int i = 0; i<s2len - 1; i++)if (s2[i]<'0') { s2[i + 1] -= 1; s2[i] += 10; } if (s2[s2len - 1] == '0')s2.erase(s2.end() - 1); reverse(s2.begin(), s2.end()); return bigInt(trimPre0(s2)); } bigInt operator * (bigInt bI2) const { string v1 = v, v2 = bI2.v; if (v2.length() > v1.length())swap(v1, v2); reverse(v1.begin(), v1.end()); reverse(v2.begin(), v2.end()); string v3 = ""; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; for (int i = 0; i < v2.length(); i++)v2[i] -= '0'; v3.resize(v1.length() + v2.length(), 0); for (int i = 0; i < v2.length(); i++) { for (int j = 0; j < v1.length(); j++) { v3[i + j] += v2[i] * v1[j]; } bIsort(v3); } for (int i = 0; i < v3.length(); i++)v3[i] += '0'; reverse(v3.begin(), v3.end()); trimPre0(v3); return bigInt(v3); } bigInt operator / (long long bI2) const { string v1 = v; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; long long v2 = bI2; string v3 = ""; long long div = 0; for (int i = 0; i < v1.length(); i++) { div *= 10; div += v1[i]; if (div < v2) { v3.append(1, '0'); continue; } v3.append(lng2str(div / v2)); div %= v2; } return bigInt(trimPre0(v3)); } bool operator < (bigInt bI2) const { if (v.length() != bI2.v.length())return v.length() < bI2.v.length(); for (int i = 0; i < v.length(); i++) { if (v[i] != bI2.v[i])return v[i] < bI2.v[i]; } return false; } friend istream& operator >> (istream& in, bigInt& bI) { in >> bI.v; return in; } friend ostream& operator << (ostream& out, bigInt& bI) { out << bI.v; return out; } }; bigInt ans=0,f[520][520]; int main(){ int k,w; cin>>k>>w; int maxLen=w/k; if(w%k!=0)maxLen++; int radix=1<<k; maxLen=min(maxLen,radix-1); for(int i=1;i<=radix-1;i++)f[1][i]=1; for(int i=2;i<=maxLen;i++){ int l; if(w%k==0)l=PINF; else if(i==maxLen)l=(1<<(w%k))-1; else l=PINF; for(int j=radix-i;j>0;j--){ f[i][j]=f[i][j+1]+f[i-1][j+1]; if(j<=l)ans=ans+f[i][j]; } } cout<<ans; } |