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

ZOJ Hypersphere 快速矩阵幂(2013 ACM/ICPC Asia Regional Changsha Online H)

Hypersphere

Time Limit: 1 Second      Memory Limit: 32768 KB

In the world of k-dimension, there's a large hypersphere made by mysterious metal. People in the world of k-dimension are performing a ceremony to worship the goddess of dimension. They are melting the large hypersphere into metal flow, and then they will cast the metal flow into unit hyperspheres. An unit hypersphere is a hypersphere which radius is 1.

The mysterious metal in k-dimension world has a miraculous property: if k unit hyperspheres made by the mysterious metal are constructed, all of these k unit hyperspheres will be sacrificed to goddess of dimension and disappear from the k-dimension world immediately.

After the ceremony, there will be some unit hyperspheres and a little metal flow left, and people in the world of k-dimension want to know the number of unit hyperspheres left.

You might want to know that how the large hypersphere was constructed. At first, people in the world created a long ruler which length is l. And then, they drew a rectangle with length l - 1 and width l. Using some mysterious method, they changed the rectangle into a square but didn't change the area. After that, they extended the ruler's length by the length of the square's side. After successfully made the ruler, people started using magic to construct the large hypersphere. The magic could make a hypersphere become more and more larger. Started with a hypersphere of zero radius, the magic will be continuously used until the radius reached the ruler's length.

Input

There will be several test cases. Each test case contains two integers k (3 ≤ k ≤ 1000000000) and l (2 ≤ l ≤ 1000000000), which are the same meanings as in the description part.

Output

For each test case, please output the number of unit hyperspheres people will get in one line.

Sample Input

3 3
5 6

Sample Output

2
1
 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <vector>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 using namespace std;
11 #define maxn 11015
12 #define INF 0x7fffffff
13 #define FF(i,n) for(int i=0;i<n;i++)
14 #define ll long long
15 //#define ll __int64
16 int n;
17 ll c[2][2],m[2][2];
18 ll mod,k,l;
19 void Mul(ll a[2][2],ll b[2][2]){
20     FF(i,n)FF(j,n)c[i][j]=0;
21     FF(k,n)FF(i,n)FF(j,n)c[i][j]=(c[i][j]+(a[i][k]%mod)*(b[k][j]%mod)%mod)%mod;
22 }
23 void Pow(ll a[2][2],ll b){
24     FF(i,n)FF(j,n)m[i][j]=(i==j);
25     while(b){
26         if(b&1){Mul(m,a);FF(i,n)FF(j,n)m[i][j]=c[i][j];}
27         Mul(a,a);FF(i,n)FF(j,n)a[i][j]=c[i][j];
28         b>>=1;
29     }
30 }
31 int main(){
32     ll mm[2][2];
33     while(~scanf("%lld%lld",&k,&l)){
34         mod=k;
35         mm[0][0]=l%mod;mm[0][1]=l*(l-1)%mod;
36         mm[1][0]=1;mm[1][1]=l%mod;
37         Pow(mm,k);
38         printf("%lld\n",(m[0][0]*2+mod-1)%mod);
39     }
40     return 0;
41 }
View Code 2013-10-01 19:46:40 

 

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