51nod 1013:3的幂的和 快速幂】的更多相关文章

1113 矩阵快速幂 链接:传送门 思路:经典矩阵快速幂,模板题,经典矩阵快速幂模板. /************************************************************************* > File Name: 51nod1113.cpp > Author: WArobot > Blog: http://www.cnblogs.com/WArobot/ > Created Time: 2017年05月01日 星期一 23时14分3…
关于快速幂这个算法,已经不想多说,很早也就会了这个算法,但是原来一直靠着模板云里雾里的,最近重新学习,发现忽视了一个重要的问题,就是若取模的数大于int型,即若为__int64的时候应该怎么办,这样就得用到乘法快速幂+乘方快速幂了. 快速幂一般是为了解决乘方取模问题的,显然思想就是二分,下面贴上快速幂模板: __int64 mulpow(__int64 a,__int64 p,__int64 m) { __int64 ans = ; while(p) { ) ans = ans * a % m;…
题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https://www.cnblogs.com/liubilan/p/9450568.html 除法取模:(a/b)%mod = (a%(b*mod))/b 当a/b比mod小,而a又比mod大的时候a先取余再除以b就会产生错误:为了避免这个错误,只需将模数乘以b即可: 这个题目其实就是找规律,n 有1e9大,不…
  D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Consider a linear function f(x) = Ax + B. Let's define g(0)(x) = x and g(n)(x) = f(g(n - 1)(x)) for n > 0. For…
1013 3的幂的和 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 求:3^0 + 3^1 +...+ 3^(N) mod 1000000007 Input 输入一个数N(0 <= N <= 10^9) Output 输出:计算结果 Input示例 3 Output示例 40 代码: #include <iostream> #include <algorithm> #include <cmath> #in…
1.乘法逆元 直接使用等比数列求和公式,注意使用乘法逆元 ---严谨,失细节毁所有 #include "bits/stdc++.h" using namespace std; #define rep(i, s, n) for(int i=s;i<n;i++) #define MOD 1000000007 #define LL long long ; LL quick_pow(LL a,LL b) { LL ans=; ){ ){ ans=ans*a%MOD; } b>>…
给出3个正整数A B C,求A^B Mod C.   例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^9) Output 输出计算结果 Input示例 3 5 8 Output示例 3 代码 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define ll l…
斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...) 给出n,求F(n),由于结果很大,输出F(n) % 1000000009的结果即可.   Input输入1个数n(1 <= n <= 10^18).Output输出F(n) % 1000000009的结果.Sample Input…
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d      C D   =   c*A+d*C  c*A+d*C 上代码 struct matrix { ll a[maxn][maxn]; }; matrix matrix_mul(matrix x,matrix y) { matrix temp; ;i<=n;i++) ;j<=n;j++) { tem…
fibonacci数列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, - An alter…