- 注册时间
- 2007-12-27
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 40149
- 在线时间
- 小时
|
发表于 2014-7-4 10:24:24
来自手机
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <vector>
- using namespace std;
- #define MAXC 100
- #define MIN_ERR 1E-10
- double f(double k, double x, int t){
- double s=0.0;
- double ki=pow(k,(double)t);
- int i;
- for(i=t;i>=0;i--){
- s+=1./(x+ki);
- ki/=k;
- }
- return s;
- }
- double df(double k, double x, int t){
- double s=0.0;
- double ki=pow(k,(double)t);
- int i;
- for(i=t;i>=0;i--){
- s-=1./((x+ki)*(x+ki));
- ki/=k;
- }
- return s;
- }
- double find_max_root(double k, int t){
- double x0;
- int c=0;
- x0=-(k+1.0)/2.0;
- while(c<MAXC){
- double err=f(k,x0,t)/df(k,x0,t);
- x0-=err;
- if(err/x0<MIN_ERR)
- break;
- }
- if(c==MAXC){
- fprintf(stderr,"too much error find max root for %f,%d\n",k,t);
- }
- return x0;
- }
- double find_min_root(double k, int t){
- double x0;
- double kp;
- int c;
- kp=pow(k,(double)t);
- x0=-(k+1.0)/2.0*kp/k;
- while(c<MAXC){
- double err=f(k,x0,t)/df(k,x0,t);
- x0-=err;
- if(err/x0<MIN_ERR)
- break;
- }
- if(c==MAXC){
- fprintf(stderr,"too much error find min root for %f,%d\n",k,t);
- }
- return x0;
- }
- double find_root_target(double k, int t, double r){
- double p=1.0;
- double kp=1.0;
- int i;
- for(i=0;i<=t;i++){
- p/=(1.0+kp*r);
- kp/=k;
- }
- return p;
- }
- #define TYPE_A 0
- #define TYPE_B 1
- typedef struct _rtype_t{
- double d;
- int c;
- int t;
- }rtype_t;
- vector<rtype_t> v;
- #define MAXN 40
- #define MINK 1.01
- #define MAXK 10.0
- #define STEPK 0.01
- typedef struct _item_t{
- short index;
- short count;
- }item_t;
- item_t cur_max[MAXN];
- int cur_max_count;
- double cur_max_value;
- item_t cur_min[MAXN];
- int cur_min_count;
- double cur_min_value;
- item_t cur_stack[MAXN];
- int cur_stack_count;
- int csum;
- double mprod[MAXN];
- void dump_items(const item_t items[MAXN], int c, double value){
- printf("%.3g\t",value);
- if(c==0){
- printf("G");
- }else{
- int i;
- for(i=0;i<c;i++){
- int index=items[i].index;
- if(v[index].t==TYPE_A){
- printf("A%d",v[index].c);
- }else{
- printf("B%d",v[index].c);
- }
- if(items[i].count>1){
- printf("^%d",items[i].count);
- }
- }
- }
- }
- void find_recursive(double k, int n, int t)
- {
- int i,j;
- int ic=v[t].c;
- double d=1.0;
- for(i=1;i*ic<=n-csum;i++){//try to use i times v[t].d
- d*=v[t].d;
- if(cur_stack_count==0){
- mprod[cur_stack_count]=d;
- }else{
- mprod[cur_stack_count]=mprod[cur_stack_count-1]*d;
- }
- cur_stack[cur_stack_count].index=t;
- cur_stack[cur_stack_count].count=i;
- csum+=i*ic;
-
- if(csum==n){
- if(mprod[cur_stack_count]<cur_min_value){
- cur_min_value=mprod[cur_stack_count];
- cur_min_count = cur_stack_count+1;
- memcpy(&cur_min, &cur_stack, sizeof(cur_min[0])*cur_min_count);
- }else if(mprod[cur_stack_count]>cur_max_value){
- cur_max_value=mprod[cur_stack_count];
- cur_max_count = cur_stack_count+1;
- memcpy(&cur_max, &cur_stack, sizeof(cur_max[0])*cur_max_count);
- }
- }else{
- cur_stack_count++;
- for(j=t+1;j<v.size();j++){
- find_recursive(k,n,j);
- }
- cur_stack_count--;
- }
- csum-=i*ic;
- }
- }
- void find_min_max(double k,int n)
- {
- cur_max_count=cur_min_count=0;
- cur_max_value=cur_min_value=1.0;
- cur_stack_count=0;csum=0;mprod[0]=1.0;
- int i;
- for(i=0;i<v.size();++i){
- find_recursive(k,n,i);
- }
- printf("min%d\t",n);
- dump_items(cur_min,cur_min_count,cur_min_value);
- printf("\tmax%d\t",n);
- dump_items(cur_max,cur_max_count,cur_max_value);
- printf("\t");
- }
- void try_add_value(const rtype_t& rt)
- {
- int size=v.size();
- int i,j;
- for(i=0;i<size;i++){
- for(j=i;j<size;j++){
- if(v[i].c+v[j].c!=rt.c)continue;
- double t=v[i].d*v[j].d;
- if(t*rt.d>0){//must same sign
- if(fabs(rt.d)<=fabs(t)){//we could remove it
- return;
- }
- }
- }
- }
- v.push_back(rt);
- }
- int main()
- {
- double k;
- int n;
-
- for(k=MINK;k<MAXK;k+=STEPK){
- v.clear();
- double r=find_min_root(k,1);//r1
- double A2=find_root_target(k,1,r);
- printf("k=%.3g\t",k);
- rtype_t rt;
- rt.d=A2;rt.c=2;rt.t=TYPE_A;
- v.push_back(rt);
- for(n=3;n<=MAXN;n++){
- r=find_min_root(k,n-1);
- double B = find_root_target(k,n-1,r);
- r=find_max_root(k,n-1);
- double A = find_root_target(k,n-1,r);
- if(n&1){
- rt.d=B;rt.c=n;rt.t=TYPE_B;
- try_add_value(rt);
- rt.d=A;rt.t=TYPE_A;
- try_add_value(rt);
- }else{
- if(fabs(A)>=fabs(B)){
- rt.d=A;rt.c=n;rt.t=TYPE_A;
- }else{
- rt.d=B;rt.c=n;rt.t=TYPE_B;
- }
- try_add_value(rt);
- }
- find_min_max(k,n);
- }
- printf("\n");
- }
- }
复制代码 |
|