zoukankan      html  css  js  c++  java
  • 【HDU4565】So Easy!

    【HDU4565】So Easy!

    题面

    要你求

    [lceil (a+sqrt b)^n ceil \% m ]

    其中(0<a,m<2^{15},(a-1)^2<b<a^2,0<b,n<2^{31})

    题解

    这个向上取整放在这里很丑对吧,我们化一下柿子。

    [ecause (a-1)^2<b<a^2\ herefore a-sqrt b ]

    因为((a-sqrt b)^n)是个很小的小数且(a-sqrt b)(a+sqrt b)共轭,所以

    [(a-sqrt b)^n+(a+sqrt b)^n=lceil (a+sqrt b)^n ceil ]

    (C_n=(a-sqrt b)^n+(a+sqrt b)^n)

    [C_n*left((a-sqrt b)+(a+sqrt b) ight) \ = (a+sqrt b)^{n+1}+(a-sqrt b)^{n+1}+(a^2-b)*(a-sqrt b)^{n-1}+(a^2-b)*(a+sqrt b)^{n-1}\ = C_{n+1}+(a^2-b)C_{n-1}\ Leftrightarrow C_{n+1}=2a*C_n+(b-a^2)C_{n-1} ]

    然后构一个矩阵,就可以快速幂了:

    [ left[ egin{matrix} 2a & b-a^2\ 1 & 0\ end{matrix} ight] * left[ egin{matrix} C_{n} \ C_{n-1} \ end{matrix} ight] = left[ egin{matrix} C_{n+1} \ C_n \ end{matrix} ight] ]

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring> 
    #include <cmath> 
    #include <algorithm> 
    using namespace std; 
    typedef long long ll; 
    ll A, B, N, Mod; 
    struct Matrix { 
    	ll m[2][2]; 
    	void clear() { memset(m, 0, sizeof(m)); } 
    	void init() { for (int i = 0; i < 2; i++) m[i][i] = 1; } 
    	ll *operator [] (int id) { return m[id]; } 
    	Matrix operator * (const Matrix &b) { 
    		Matrix res; res.clear(); 
    		for (int i = 0; i < 2; i++)
    			for (int j = 0; j < 2; j++)
    				for (int k = 0; k < 2; k++) 
    					res[i][j] = (res[i][j] + m[i][k] * b.m[k][j] % Mod) % Mod; 
    		return res; 
    	} 
    } S, T; 
    int main () { 
    	while (~scanf("%lld%lld%lld%lld", &A, &B, &N, &Mod)) { 
    		S.clear(), T.clear(); 
    		S[0][0] = (A * 2) % Mod, S[1][0] = 2; 
    		T[0][0] = (A * 2) % Mod, T[0][1] = ((B - A * A % Mod) % Mod + Mod) % Mod; 
    		T[1][0] = 1, T[1][1] = 0;
    		Matrix res; res.clear(); res.init(); 
    		while (N) { if (N & 1) res = res * T; N >>= 1; T = T * T; } 
    		S = res * S; 
    		printf("%lld
    ", (S[1][0] % Mod + Mod) % Mod); 
    	} 
    	return 0; 
    } 
    
  • 相关阅读:
    Ognl表达式基本原理和使用方法
    Struts的拦截器
    Struts框架的核心业务
    Struts2框架基础
    Struts的文件上传下载
    .JavaWeb文件上传和FileUpload组件使用
    mysql数据库连接与锁查询
    关于MyBatis的@Mapper和@MapperScan注解的一点思考
    Hystrix报错java.util.concurrent.TimeoutException: null
    The Hystrix timeout of 2000ms for the command xxx is set lower than the combination of the Ribbon read and connect timeout, 4000ms.
  • 原文地址:https://www.cnblogs.com/heyujun/p/10297453.html
Copyright © 2011-2022 走看看