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:

Here are the nine battlefields for p=10:

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
p
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 }
浙公网安备 33010602011771号