acm题目,一直WA,求大神改错

u010709918 2013-05-23 05:27:15
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define N 2
#define M 12
#define MOD 100000007LL
#define ll long long

struct node
{
int pos,id;
bool operator<(const node&aa)const
{
return pos<aa.pos;
}
}po[M];

struct Matrix
{
ll a[N][N];
}mat[32];
Matrix Mul(Matrix p,Matrix q)
{
int i,j,k;
Matrix ret;
memset(ret.a,0,sizeof(ret.a));
for(k=0;k<N;++k){
for(i=0;i<N;++i){
for(j=0;j<N;++j) ret.a[i][j]=(ret.a[i][j]+p.a[i][k]*q.a[k][j])%MOD;
}
}
return ret;
}
void Initmat()
{
mat[0].a[0][0]=mat[0].a[1][1]=1;
mat[0].a[0][1]=mat[0].a[1][0]=0;
mat[1].a[0][0]=2; mat[1].a[0][1]=1;
mat[1].a[1][0]=3; mat[1].a[1][1]=0;
for(int i=2;i<32;++i) mat[i]=Mul(mat[i-1],mat[i-1]);
}
Matrix PowerMat(ll n)
{
Matrix ret=mat[0];
for(int i=1;n;++i,n/=2)
{
if(n%2) ret=Mul(ret,mat[i]);
}
return ret;
}
ll PowerNum(ll n,ll m)
{
ll ret=1,tt=n;
for(;m;tt=(tt*tt)%MOD,m>>=1)
{
if(m%2) ret=(ret*tt)%MOD;
}
return ret;
}

int main()
{
int n,m,i;
ll ans,g1=2,f1=3;
char str[20];
Matrix ret;
Initmat();
while(scanf("%d%d",&n,&m)==2)
{
for(i=0;i<m;++i){
scanf("%d%s",&po[i].pos,str);
po[i].id=str[0]-'A';
}
if(m==0){
printf("%lld\n",(PowerNum(3,n-1)*4)%MOD);
continue;
}
sort(po,po+m);
ans=PowerNum(3,po[0].pos-1+n-po[m-1].pos);
for(i=1;i<m;++i)
{
if(po[i].pos-po[i-1].pos>1)
{
ret=PowerMat(po[i].pos-po[i-1].pos-2);
if(po[i].id==po[i-1].id) ans=(ans*(ret.a[1][0]*g1+ret.a[1][1]*f1)%MOD)%MOD;
else ans=(ans*(ret.a[0][0]*g1+ret.a[0][1]*f1)%MOD)%MOD;
}
else{
if(po[i].id==po[i-1].id) break;
}
}
if(i<m) puts("0");
else printf("%lld\n",ans);
}
return 0;
}

题目是ZOJ的3538,http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4473
不知哪里WA了。。
...全文
129 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010709918 2013-05-25
  • 打赏
  • 举报
回复
引用 1 楼 hugett 的回复:
#define MOD 1000000007LL
你少了1个0。。
多谢大神。。。竟然犯了这么NC的错误。。
hugett 2013-05-24
  • 打赏
  • 举报
回复
#define MOD 1000000007LL
你少了1个0。。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧