Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that…
Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5934   Accepted: 3461 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth…
http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; ll mod_pow(ll x,ll n,ll mod) { ll res=; ) { ) res=res*x%mod; x=x*x%mod; n>>=; } return res; }…
题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11  11=2^0+2^1+2^3    => 3^1*3^2*3^8=3^11 实现代码: int solve(int a,int b) { int ans=1;          while(b){ if(b&1)  ans=ans*a;   a=a*a;  b>>=1;} } 解释一下代码:b&1即二进制表达式的最后一位,  11二…
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a): //POJ #include <cstdio> #include <iostream> using namespace std; ; struct matrix { ][]; }ans, base; matrix multi(matrix a, matrix b) { matrix…
题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k),k为所有能取到1的情况.首先我们要确认最后1的奇偶性.因为一次翻转0->1,或者1->0,则最后所有1的情况的奇偶性相同.然后我们要找到最小的1的个数i和最大的1的个数j,i为能翻1则翻1,j为能翻0则翻0,介于中间的情况是取偶数步数,一半翻1,一半翻0,保持1的个数不变.那么k为(i<=…
链接:https://ac.nowcoder.com/acm/contest/549/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 小A来到了一个陌生的城镇,这个城镇与其它城镇之间构成了集群.城镇之间的路径都是单向的,而小A每一天都能由一个城镇走到另外一个城镇.小A将会连续走k天,直到抵达某个城镇.也许他并不能走到这个城镇,那么可以认为不存在这样的路径,也就是路径数为0…
n=1  --> ans = 2 = 1*2 = 2^0(2^0+1) n=2  -->  ans = 6 = 2*3 = 2^1(2^1+1) n=3  -->  ans = 20 = 4*5 = 2^2(2^2+1) n=4  -->  ans = 72 = 8*9 = 2^3(2^3+1) n=k  -->  ??? = 2^k-1*(2^k-1+1) 于是题目转化为快速幂求模问题..... #include<bits/stdc++.h> using nam…
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of c…
题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什么国家队集训队论文什么的,自己没去看那个论文,就说下我自己的理解吧,对于这个题目,我们首先分析下Floyd,那个算法的过程中是在更新的dis[i][j]上再更新,再更新...,是想一下,我们每次都把更新的结果存下来,就是每次答案数组初始化全是INF,然后用当前的dis数组和原始的map来更新,那么更…
Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1845 Appoint description:   System Crawler  (2015-05-27) Description Consider two natural numbers A and B. Let S be the sum of all natural…
题面 \(solution:\) 首先,如果题目只要我们求\(A^K\) 那这一题我们可以直接模版矩乘快速幂来做,但是它现在让我们求$\sum_{i=1}^{k}{(A^i)} $ 所以我们思考一下这两者是否有什么关系.仔细一想,不难发现几个东西: 一次矩阵乘法复杂度为\(O(n^3)\),所以我们不能进行太多次矩阵乘法 快速幂的复杂度为\(O(logk)\) 再乘一下矩阵乘法的复杂度,我们现在只能再接受\(O(log)\)级别的处理了 矩阵乘法满足交换律和结合律!!!! 若我们已经知道了\(A…
HDU4686 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意:题目说的很清楚了,英语不好的猜也该猜懂了,就是求一个表达式的前n项和,矩阵快速幂一般多加一行一列来完成这个加的操作.具体看代码吧.比较简单,唯一有一点坑的地方,就是ax和bx可能比较大,在求ax*bx的时候,要考虑溢出的问题,需要先mod.其他没有了,直接看代码吧! //Author: xiaowuga #include <bits/stdc++.h> #define…
小明的求助 时间限制:2000 ms  |  内存限制:65535 KB 难度:2 描述 小明对数学很有兴趣,今天老师出了道作业题,让他求整数N的后M位,他瞬间感觉老师在作弄他,因为这是so easy! 当他看到第二道题目的时候,他就确定老师在捉弄他了,求出N^P的后M位,因为他不会了.你能帮他吗? 输入 第一行包含一个整数T(T <= 1000),代表测试数据组数. 接下来的T行每行含三个整数,N,P,M(1 <= N <= 10^10,1 <= P <= 10^15,1…
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(109). #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include&…
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 39554    Accepted Submission(s): 14930 Problem Description Given a positive integer N, you should output the most right digit of N…
  Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 9368 Description 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 seque…
Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10) for all n bigger than zero. Please calculate f(n)%m. (2 ≤ n , m ≤ 10^9, x^y means the y th power of x). InputThe first line contains a single positive integer T. which is the number of test cases.…
Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 187893    Accepted Submission(s): 46820 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A…
快速幂也是比较常用的,原理在下面用代码解释,我们先看题. 51Nod1046 A^B Mod C 给出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 输出计算结果 Sample Input 3 5 8 Sample Output 3 这里开始我用的是int型,一直错,因为a.b.c范围比较大,所以中间可能溢出,后来用了long long才过.…
http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> #include<cmath> using namespace std; typedef long long ll; bool judge_prime(ll k) { ll i; ll u=int(sqrt(k*1.0)); ;i<=u;i++) { ) ; } ; } ll mod_p…
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vect…
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> using namespace std; ; struct Matrix { ][]; Matrix() { memset(a, , sizeof(a)); } Matrix operator * (const Matrix y) { Matrix ans; ; i <= ; i++) ; j <=…
原理 我们取矩阵A 则 F1=F2=1;则可以轻易求出F(i) #define maxn 2 #define mo 1000000007 struct Matrix{ long long a[maxn][maxn]; Matrix(){ memset(a,,sizeof(a)); } Matrix operator * (const Matrix& c)const{ Matrix b; ;i<maxn;i++){ ;j<maxn;j++){ ;k<maxn;k++){ b.a[i…
#include <cstring> #include <cstdio> #include <iostream> #include <cmath> #include <algorithm> using namespace std; #define LL long long LL p,res,a; bool judge_prime(LL k) { LL i; LL u=int(sqrt(k*1.0)); ;i<=u;i++) { ) ; }…
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a ps…
先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n -= 1 while(n > 0): if (n % 2 == 1): a = np.dot(b, a) b = np.dot(b, b) n >>= 1 return a[0][0] n = int(input()) print(factorial(n))…
参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其实也不是很好看出规律的...而且这个表都写了半天233...(真是太弱了= =)为了打表,我们应当先知道k数列必须是不递减的才能满足值不为0,因此我们可以用递归来写这个表(类似于dfs). AC代码如下: #include <stdio.h> #include <algorithm>…
#include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; typedef long long LL; struct Mat { LL mat[3][3]; Mat() { memset(mat,0,sizeof(mat)); } LL* operator [](int x) //注意这种写法 { return mat[x]; } } A; Mat Mut(Mat a,Mat…
1046 A^B Mod C   给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. 收起   输入 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^9) 输出 输出计算结果 输入样例 3 5 8 输出样例 3 分治法,注意要用long long,防止数字溢出C++代码: #include<iostream> #include<cstdio> using namespace std; int pow…