首先有个关键性的结论就是一个数的合数幂就是超级幂. 最小的合数是4,所以枚举底数的上限是pow(2^64, 1/4) = 2^16 = 65536 对于底数base,指数的上限就是ceil(64*log(2)/log(base)),注意这个上限不能取到,是个开区间 #include <cstdio> #include <cmath> #include <set> #include <cassert> using namespace std; typedef…
题意:给你一个数,让你判断是否是非素数,同时a^n%n==a (其中 a 的范围为 2~n-1) 思路:先判断是不是非素数,然后利用快速幂对每个a进行判断 代码: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #define ll long long using namespace std; bool isprime(ll num) { ) return…
传送门:http://poj.org/problem?id=1845 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. ,其中为素数 2) 约数和公式: 对于已经分解的整数,A的所有因子之和为 3) 同余模公式: (a+b)%m=(a%m+b%m)%m (a*b)%m=(a%m*b%m)%m 1: 对A进行素因子分解 这里如果先进行筛50000内的素数会爆空间,只能用最朴素的…
In a galaxy far far away there is an ancient game played among the planets. The specialty of the gameis that there is no limitation on the number of players in each team, as long as there is a captain inthe team. (The game is totally strategic, so so…
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) ..... f(d),求 f(n) = a1*f(n-1) + a2*f(n-2) +....+ ad*f(n-d),计算f(n) % m. 析:很明显的矩阵快速幂,构造矩阵, ,然后后面的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1…
https://vjudge.net/problem/UVA-11609 题意: 有n个人,选一个或多个人参加比赛,其中一名当队长,有多少种方案?如果参赛者完全相同,但队长不同,算作不同的方案. 思路: 之后就是快速幂处理. #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<vector> #include<queue>…
题目大意: f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d),已给递推公式,求f(n)的大小. 解题思路: n很大,所以我们就要构造矩阵,运用矩阵快速幂来求解.//题目描述上口口声声说int范围内,但是大家一定不要天真!!!!!! #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm…
//求x^nint ans=1;while(n){ if(n&1) ans=ans*x; x*=x; n>>=1;} 快速幂就是快速算底数的n次幂.其时间复杂度为 O(logN), 与朴素的O(N)相比效率有了极大的提高. 例如 11的二进制是1011 11 = 2³×1 + 2²×0 + 2¹×1 + 2º×1 因此,我们将a¹¹转化为算 下面看一个例题…
Recurrences Input: standard input Output: standard output Consider recurrent functions of the following form: f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d. a1, a2, ..., ad - arbitrary constants. A famous example is th…
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> using namespace std; ]; ]; ]; int num; void Init() { int i,j; num=; memset(flag,true,sizeof(flag)); flag[]=flag[]=; ;i<;i++) { if(flag[i]) prime[num++]=i; ;j&…