[HDU4990] 等比公式+取模运算公式 或矩阵求递推法~
来源:互联网 发布:淘宝55寸电视才一千多 编辑:程序博客网 时间:2024/04/27 21:18
Reading comprehension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 384 Accepted Submission(s): 185
Problem Description
Read the program below carefully then answer the question.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
}
Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
[Technical Specification]
1<=n, m <= 1000000000
Output
For each case,output an integer,represents the output of above program.
Sample Input
1 103 100
Sample Output
15
Source
BestCoder Round #8
Recommend
heyang | We have carefully selected several similar problems for you: 4992 4988 4987 4984 4983
打个表,递推公式 a[n]=a[n-1]+2*a[n-2]+1
建立矩阵 [a[n-1],a[n-2],1]*[1,1,0]
[2,0,0]
[1,0,1]
【a[2],a[1],1】* 后面矩阵的 n-2次幂 n=1,2的时候特判就行
注意爆int
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <list>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6#define TRUE true#define FALSE falsetypedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){ return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 100005const int MAXN = 5;long long mod;struct Matrix{ long long mat[MAXN][MAXN]; void Zero() { memset(mat, 0, sizeof(mat)); } void Unit() { memset(mat, 0, sizeof(mat)); for (int i = 0; i < MAXN; i++) mat[i][i] = 1; } void output() { for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { printf("%d ", mat[i][j]); } printf("\n"); } }};Matrix operator*(Matrix &a, Matrix &b){ Matrix tmp; tmp.Zero(); for (int k = 0; k < MAXN; k++) { for (int i = 0; i < MAXN; i++) { if (!a.mat[i][k]) continue; for (int j = 0; j < MAXN; j++) { tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j] % mod; if ( tmp.mat[i][j] >= mod) tmp.mat[i][j] -= mod; } } } return tmp;}Matrix operator ^(Matrix a, int k){ Matrix tmp; tmp.Unit(); if(k<=1) return a; for (; k; k >>= 1) { if (k & 1) tmp = tmp * a; a = a * a; } return tmp;}int main(){#ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif Matrix mt; long long n,m; while (scanf("%I64d%I64d", &n, &m) + 1) { mod=m; if(n==1) { printf("%I64d\n", 1%m); continue; } else if(n==2) { printf("%I64d\n", 2%m); continue; } mt.Zero(); mt.mat[0][0] = 1; mt.mat[0][1] = 1; mt.mat[1][0] = 2; mt.mat[2][0] = 1; mt.mat[2][2] = 1; mt = mt ^ (n-2); Matrix mt2; mt2.Zero(); mt2.mat[0][0] = 2; mt2.mat[0][1] = 1; mt2.mat[0][2] = 1; Matrix ans = mt2 * mt; printf("%I64d\n", ans.mat[0][0]); } return 0;}
推公式,公式看程序就懂了,两个等比数列其实 关键求(4^dm -1)/3 %mod
利用公式a/b%m = a%(b*m)/b注意这时候再取模是%(b*m)快速幂的时候得变了,因为这个逗B了。。。
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <list>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6#define TRUE true#define FALSE falsetypedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){ return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 100005long long MOD;LL pow2(LL a, LL b, LL mod){ LL r = 1, base = a; while (b != 0) { if (b % 2) r = r * base % mod; base = base * base % mod; b /= 2; } return r;}int main(){#ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif long long n, m; while (scanf("%I64d%I64d", &n, &m) + 1) { MOD = m; int dm; if (n & 1) dm = (n - 1) / 2 + 1; else dm = (n - 2) / 2 + 1; long long ans = (pow2(4, dm, (3 * MOD)) - 1) % (3 * MOD) / 3; if (n % 2 == 0) { ans = (ans * 2) % MOD; } printf("%I64d\n", ans); } return 0;}
0 0
- [HDU4990] 等比公式+取模运算公式 或矩阵求递推法~
- 取余运算推导公式
- 取模公式
- 等比数列取模求和公式(数字,矩阵皆可)
- uva10056 等比序列求和公式
- 等差等比数例求和公式
- CF 327C 数学题 等比公式
- LaTex编译公式 方程组大括号、矩阵运算
- HDU 6050 Funny Function(矩阵快速幂或公式)
- 大数取模公式的理解
- 取余公式
- 矩阵求导公式
- 矩阵求导公式
- 矩阵求导公式
- 矩阵求导公式
- 矩阵求导公式
- 矩阵求导公式
- 常用矩阵微分公式
- eclipse android 真机调试 简略步骤
- 姚博文 linux解压gbk文件
- 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- 码分多址(CDMA)的本质-正交之美
- QProgressBar 不停的跑的效果
- [HDU4990] 等比公式+取模运算公式 或矩阵求递推法~
- ThreadLocal分析总结(转自iteye http://www.iteye.com/topic/777716)
- 面试题6 先序和中序建二叉树
- 怎么学???学什么??求大神指教!
- GPS坐标系与实际地图坐标系
- 动态规划专题
- java socket单线程阻塞
- Lua基础字符串操作
- jstl fn:replace替换换行符