【CUGBACM15级BC第8场 B】hdu 4990 Reading comprehension
来源:互联网 发布:淘宝怎么寄东西到澳洲 编辑:程序博客网 时间:2024/06/08 02:51
Reading comprehension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2072 Accepted Submission(s): 827
Total Submission(s): 2072 Accepted Submission(s): 827
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
思路:
先用源程序跑几个数出来!1, 2, 5, 10, 21, 42 好了,再用这几个数字找他们的通项式#include <iostream>#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <algorithm>#include <functional>#define PI acos(-1)#define eps 1e-8#define inf 0x3f3f3f3f#define debug(x) cout<<"---"<<x<<"---"<<endltypedef long long ll;ll mod;struct matrix{ ll mat[5][5]; matrix() { memset(mat, 0, sizeof(mat)); } int R, C; matrix operator*(matrix other);};matrix matrix::operator*(matrix other){ matrix c; for (int i = 1; i <= R; i++) for (int j = 1; j <= C; j++) for (int k = 1; k <= C; k++) { c.mat[i][j] = (c.mat[i][j] + mat[i][k] * other.mat[k][j] % mod) % mod; } c.R = R; c.C = other.C; return c;}matrix fast_mod(matrix base, int k){ matrix tmp; tmp.R = tmp.C = 3; for (int i = 1; i <= 3; i++) { tmp.mat[i][i] = 1; } while (k) { if (k & 1) { tmp = tmp * base; } base = base * base; k >>= 1; } return tmp;}int main(){ int n; while (~scanf("%d%I64d", &n, &mod)) { matrix ans, base; ans.R = 1; ans.C = 1; base.R = base.C = 3; if (n == 1) { printf("%d\n", 1 % mod); } else if (n == 2) { printf("%d\n", 2 % mod); } else { ans.R = 1; ans.C = 3; ans.mat[1][1] = 1; ans.mat[1][2] = 2; ans.mat[1][3] = 1; base.mat[2][3] = base.mat[1][1] = base.mat[1][2] = base.mat[1][3] = 0; base.mat[2][1] = 2; base.mat[2][2] = 4; base.mat[3][1] = base.mat[3][3] = 1; base.mat[3][2] = 2; int k = (n - 2) / 2; if (n & 1) { base = fast_mod(base, k + 1); ans = ans * base; printf("%I64d\n", ans.mat[1][1]); } else { base = fast_mod(base, k); ans = ans * base; printf("%I64d\n", ans.mat[1][2]); } } } return 0;}
阅读全文
0 0
- 【CUGBACM15级BC第8场 B】hdu 4990 Reading comprehension
- HDU 4990 / BC 8B Reading comprehension
- 【CUGBACM15级BC第15场 B】hdu 5082 Instruction
- 【CUGBACM15级BC第17场 B】hdu 5101 Select
- 【CUGBACM15级BC第20场 B】hdu 5124 lines
- 【CUGBACM15级BC第26场 B】hdu 5159 Card
- 【CUGBACM15级BC第28场 B】hdu 5167 Fibonacci
- 【CUGBACM15级BC第36场 B】hdu 5199 Gunner
- 【CUGBACM15级BC第8场 A】hdu 4989 Summary
- 【CUGBACM15级BC第12场 B】hdu 5059 Help him
- 【CUGBACM15级BC第11场 B】hdu 5055 Bob and math problem
- 【CUGBACM15级BC第13场 B】hdu 5063 Operation the Sequence
- 【CUGBACM15级BC第18场 B】hdu 5105 Math Problem
- 【CUGBACM15级BC第22场 B】hdu 5143 NPY and arithmetic progression
- 【CUGBACM15级BC第23场 B】hdu 5147 Sequence II
- 【CUGBACM15级BC第25场 B】hdu 5155 Harry And Magic Box
- 【CUGBACM15级BC第27场 B】hdu 5163 Taking Bus
- 【CUGBACM15级BC第10场 B】hdu 5019 Revenge of GCD
- go context专题(四)- context 最佳实践和相关争议
- 身为JAVA工作者必须了解的实战知识(七)
- Servlet的生命周期01
- redis之事务&&主从复制
- 初识c#---属性和参数传递
- 【CUGBACM15级BC第8场 B】hdu 4990 Reading comprehension
- redis搭建
- 身为JAVA工作者必须了解的实战知识(八)
- FileOutputStream、FileInputStream和BufferedOutputStream、FileInputStream 应用
- oracle 12c alert日志目录位置
- 身为JAVA工作者必须了解的实战知识(九)
- MYSQL group by排重用法
- hdu 1247
- 谷歌调试axios异步请求XMLHttpRequest cannot load http问题解决