- 注册时间
- 2007-12-27
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 41287
- 在线时间
- 小时
|
发表于 2020-10-28 12:35:00
|
显示全部楼层
-
- #include <map>
- using namespace std;
- typedef struct _PC{
- char cnt[3];
- bool operator<(const struct _PC& x)const{
- int i;
- for(i=0;i<3;i++){
- if(cnt[i]<x.cnt[i])return true;
- if(cnt[i]>x.cnt[i])return false;
- }
- return false;
- }
- bool operator==(const struct _PC& x)const{
- int i;
- for(i=0;i<3;i++){
- if(cnt[i]!=x.cnt[i])return false;
- }
- return true;
- }
- }PC;
- typedef map<PC, long> PCMAP;
- PCMAP pcmap0, pcmap1;
- void add_data(PCMAP *next_map, const PC& data, long count)
- {
- PCMAP::iterator it = next_map->find(data);
- if(it==next_map->end()){
- next_map->insert(std::make_pair(data, count));
- }else{
- it->second+=count;
- if(it->second<0L){
- printf("overflow\n");
- }
- }
- }
- #define N 8
- int main()
- {
- PCMAP *prev, *next, *tmp;
- int i;
- prev=&pcmap0;
- next=&pcmap1;
- PC pc;
- pc.cnt[0]=1;pc.cnt[1]=pc.cnt[2]=0;
- prev->insert(std::make_pair(pc, 1L));
- for(i=2;i<=3*N;i++){
- next->clear();
- PCMAP::iterator it;
- for(it=prev->begin();it!=prev->end();++it){
- const PC& orig = it->first;
- PC update = orig;
- if(update.cnt[0]<N){
- update.cnt[0]++;
- add_data(next, update, it->second);
- }
- if(orig.cnt[0]>orig.cnt[1]){
- update=orig;
- update.cnt[1]++;
- add_data(next,update, it->second);
- }
- if(orig.cnt[1]>orig.cnt[2]){
- update=orig;
- update.cnt[2]++;
- add_data(next,update, it->second);
- }
- }
- tmp=prev;
- prev=next;
- next=tmp;
- }
- long sum = 0;
- PCMAP::iterator it;
- for(it=prev->begin();it!=prev->end();++it){ sum+=it->second;}
- printf("Total %ld\n",sum);
- }
复制代码 |
|