CSUOJ 1895 Apache is late again
Description
Apache is a student of CSU. There is a math class every Sunday morning, but he is a very hard man who learns late every night. Unfortunate, he was late for maths on Monday. Last week the math teacher gave a question to let him answer as a punishment, but he was easily resolved. So the math teacher prepared a problem for him to solve. Although Apache is very smart, but also was stumped. So he wants to ask you to solve the problem. Questions are as follows:You can find a m made (1 + sqrt (2)) ^ n can be decomposed into sqrt (m) + sqrt (m-1), if you can output m% 100,000,007 otherwise output No.
Input
There are multiply cases.Each case is a line of n. (|n| <= 10 ^ 18)
Output
Line, if there is no such m output No, otherwise output m% 100,000,007.
Sample Input
2
Sample Output
9
Hint
题目大意:给一个n,判断是否存在m使(1 + sqrt (2)) ^ n= sqrt (m) + sqrt (m-1)先列前面几项:(1 + sqrt (2)) ^1 =sqrt (1*1+1) + sqrt (1)(1 + sqrt (2)) ^ 2=sqrt (3*3) + sqrt (8)(1 + sqrt (2)) ^ 3=sqrt (7*7+1) + sqrt (49)(1 + sqrt (2)) ^ 4=sqrt (17*17) + sqrt (288)...an = 2 * an-1 +an-2可以推出当n为奇数时m=an*an+1n为偶数时 m=an*anan可以用矩阵来表示[an an-1]T =[ (2 1) (1 0)]*[an-2 an-1]T=[(2 1) (1 0)]^n-2 *[a2 a1]
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int MOD = 100000007; typedef long long ll; struct matrix{ ll v[2][2]; matrix() { memset(v, 0, sizeof(v)); } matrix operator*(const matrix &m) { matrix c; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { c.v[i][j] += (v[i][k] * m.v[k][j]) % MOD; } } } return c; } }; matrix E, M, ans; void init() { for (int i = 0; i < 2; i++) E.v[i][i] = 1; M.v[0][0] = 2; M.v[0][1] = 1; M.v[1][0] = 1; M.v[1][1] = 0; } matrix quick_pow(matrix x, ll y) { matrix tmp = E; while (y) { if (y & 1) { tmp =tmp* x; y--; } y >>= 1; x = x*x; } return tmp; } int main() { ll n; init(); while (~scanf("%lld", &n)) { if (n < 0) printf("No\n"); else if (n == 0) printf("1\n"); else if (n == 1) printf("2\n"); else if (n == 2) printf("9\n"); else { ans = quick_pow(M, n - 2); ll a = (ans.v[0][0] * 3 + ans.v[0][1]) % MOD; if (n & 1) printf("%lld\n", ((a*a)%MOD + 1) % MOD); else printf("%lld\n", (a*a)%MOD); } } return 0; } /********************************************************************** Problem: 1895 User: leo6033 Language: C++ Result: AC Time:8 ms Memory:2024 kb **********************************************************************/
- 中南大学第十一届大学生程序设计竞赛-COJ1895-Apache is late again
- CSU 1895 Apache is late again(根号2加1的n次方)
- registerForRemoteNotificationTypes: is not supported in iOS 8.0 and late
- The Apache Tomcat installation at this directory is version 8.5.6. A Tomcat 8.0 installation is expe
- eclipse中添加tomcat8:The Apache Tomcat installation at this directory is version 8.5.11. A Tomcat 8.0 i
- jsp 自定义标签 错误 : Unable to compile class for JSP: ,apache cannot be resolved or is not a field
- shiro学习随笔【四】session过期报 org.apache.shiro.session.UnknownSessionException: There is no session with id
- It is time to work again
- ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
- nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
- Servlet of class org.apache.catalina.servlets.InvokerServlet is privileged
- org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderExce
- The Apache Tomcat installation at this directory is version 8.5.14. A Tomcat 8.0installation is expe
- iOS 提交代码出现提示弹出框显示 “A commit message is required to perform this operation.Enter a commit message and try again.“
- 解决mapper绑定异常:nested exception is org.apache.ibatis.binding.BindingException
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'cla
- Apache Commons Lang的StringUtils.isEmpty(STR)和StringUtils.isBlank(STR)
- Page is already covered by another Data Binder. Cannot be covered by this Data Binder again
- Target runtime Apache Tomcat 5.0 is not defined
- The type org.apache.tools.ant.Task cannot be resolved. It is indirectly referenced from required .cl