• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 11885 - Number of Battlefields

你为何这么叼!

Number of Battlefields 

Time limit: 1.000 seconds

 

In the previous problem, we assume the perimeter of the figure equals to p, how many battlefields are possible? For example, there are no battlefields possible for p < 8, but two for p = 8:

\epsfbox{p11885a.eps}

Here are the nine battlefields for p=10:

\epsfbox{p11885b.eps}

You're asked to output the number of battlefields modulo 987654321.

Input 

There will be at most 25 test cases, each with a single integer p ( 1$ \le$p$ \le$109), the perimeter of the battlefield. The input is terminated by p = 0.

Output 

For each test case, print a signle line, the number of battlefields, modulo 987654321.

Sample Input 

8
9
10
0

Sample Output 

0
2
0
9

 


Problemsetter: Rujia Liu, Special Thanks: Yiming Li, Tanaeem M Moosa & Sohel Hafiz

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define mod 987654321
 6 #define ll long long
 7 #define FF(i,n) for(int i=0;i<n;i++)
 8 int n=2;
 9 ll x;
10 struct mat{ll m[2][2];};
11 mat Mul(mat a,mat b){
12     mat res;
13     FF(i,n)FF(j,n)res.m[i][j]=0;
14     FF(i,n)FF(j,n)FF(k,n)res.m[i][j]=(res.m[i][j]+a.m[i][k]*b.m[k][j]%mod)%mod;
15     return res;
16 }
17 mat Pow(mat a,ll b){
18     mat res;
19     FF(i,n)FF(j,n)res.m[i][j]=(i==j);
20     while(b){
21         if(b&1)res=Mul(res,a);
22         a=Mul(a,a);
23         b>>=1;
24     }
25     return res;
26 }
27 int main(){
28     while(scanf("%lld",&x)&&x){
29         mat A;
30         if((x&1)||x<4){printf("0\n");continue;}
31         A.m[0][0]=A.m[0][1]=A.m[1][0]=1;A.m[1][1]=0;
32         A=Pow(A,x-4);
33         printf("%lld\n",(A.m[0][0]-x/2+1+mod)%mod);
34     }
35     return 0;
36 }
View Code 2013-10-10 00:09:37 

 

posted @ 2013-10-10 00:11  HaibaraAi  阅读(328)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3