您的位置:首页 > 其它

matrix_world_final_2012

2015-11-08 14:56 357 查看
B http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98759#problem/B

题意:瓶子侧躺在数轴上,瓶底在xlow,瓶口在xhigh,瓶身的曲线是多项式函数,给出a0--an是多项式系数,求瓶子的体积,和每增加 inc 体积的刻度值,最多输出8个。

解法:体积用积分求,刻度值用二分求。

//#define debug
//#define txtout
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const int M=2e5+10;
struct G {
int len;
char str[M];
} g[32];
struct S{
char prefix[M],suffix[M];
}A,B,C;
char a[M];
char buffer[M];
int res[M];
LL dp[128];
int n;
class KMP { ///模式匹配(kmp)O(ls+lp)
typedef char typec;///文本元素的类型
static const int MV=1e6+10;///字符串的长度
int next[MV];
public:///匹配串长度ls,str 存待匹配文本,模式串长度lp,pat 存模式串
int kmp(int ls,typec str[],int lp,typec pat[],int res[]) { ///返回匹配次数,res 中
//        - 31 -
//        存储每个匹配的初始位置
int i=0,j=-1,cnt=0;
next[0]=-1;
while(i<lp) {
if(j==-1||pat[i]==pat[j]) {
next[++i]=++j;
continue;
}
j=next[j];
}
i=j=0;
while(i<ls) {
while(j!=lp&&str[i]==pat[j]) {
i++;
j++;
}
if(!j) {
i++;
continue;
}
if(j==lp) {
res[cnt++]=i-j;
}
j=next[j];
}
return cnt;
}
} gx;
void init() {
g[0].len=1;
strcpy(g[0].str,"0");
g[1].len=1;
strcpy(g[1].str,"1");
for(int i=2; i<=26; i++) {
g[i].len=g[i-1].len+g[i-2].len;
strcpy(g[i].str,g[i-1].str);
strcat(g[i].str,g[i-2].str);
}
}
int get_first_big_id(int len){
for(int i=0;i<=26;i++){
if(g[i].len>=len) return i;
}
}
void init_dp(int id,S &s){
int la=strlen(a);
dp[id]=gx.kmp(g[id].len,g[id].str,la,a,res);
for(int i=0,j=g[id].len-la+1;i<la-1;i++,j++){
s.prefix[i]=g[id].str[i];
s.suffix[i]=g[id].str[j];
}
s.prefix[la-1]=0;
s.suffix[la-1]=0;
}
LL solve() {
int la=strlen(a);
if(n<=26) {
return gx.kmp(g
.len,g
.str,la,a,res);
}
int id=get_first_big_id(la);
init_dp(id,A);
init_dp(id+1,B);
id+=2;
while(id<=n){
dp[id]=dp[id-1]+dp[id-2];
strcpy(buffer,B.suffix);
strcat(buffer,A.prefix);
dp[id]+=gx.kmp(strlen(buffer),buffer,la,a,res);
strcpy(C.prefix,B.prefix);
strcpy(C.suffix,A.suffix);
A=B;
B=C;
id++;
}
return dp
;
}
int main() {
#ifdef txtout
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
init();
int cas=1;
while(~scanf("%d%s",&n,a)) {
printf("Case %d: %lld\n",cas++,solve());
}
return 0;
}


View Code

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: