2016百度之星 hdu 5690 矩阵快速幂
来源:互联网 发布:nginx 1.8 域名配置 编辑:程序博客网 时间:2024/06/08 15:20
链接:戳这里
All X
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
F(x,m) 代表一个全是由数字x组成的m位数字。请计算,以下式子是否成立:
F(x,m) mod k ≡ c
Input
第一行一个整数T,表示T组数据。
每组测试数据占一行,包含四个数字x,m,k,c
1≤x≤9
1≤m≤1010
0≤c<k≤10,000
Output
对于每组数据,输出两行:
第一行输出:"Case #i:"。i代表第i组测试数据。
第二行输出“Yes” 或者 “No”,代表四个数字,是否能够满足题目中给的公式。
Sample Input
3
1 3 5 2
1 3 5 1
3 5 99 69
Sample Output
Case #1:
No
Case #2:
Yes
Case #3:
Yes
Hint
对于第一组测试数据:111 mod 5 = 1,公式不成立,所以答案是”No”,而第二组测试数据中满足如上公式,所以答案是 “Yes”。
思路:
先用矩阵快速幂求出m个x,然后直接乘y%k==c
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;ll x,m,mod,c;struct Matrix{ ll ma[2][2]; Matrix(){ for(int i=0;i<2;i++) for(int j=0;j<2;j++) ma[i][j]=0; }};Matrix Mult(Matrix a,Matrix b){ Matrix c; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ for(int k=0;k<2;k++){ c.ma[i][j]=(c.ma[i][j]+a.ma[i][k]*b.ma[k][j]%mod)%mod; } } } return c;}Matrix MPow(Matrix a,ll b){ Matrix tmp; for(int i=0;i<2;i++) tmp.ma[i][i]=1; while(b){ if(b%2) tmp=Mult(tmp,a); a=Mult(a,a); b/=2; } return tmp;}int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ scanf("%I64d%I64d%I64d%I64d",&x,&m,&mod,&c); printf("Case #%d:\n",cas); Matrix a,b; a.ma[0][0]=10;a.ma[0][1]=1;a.ma[1][0]=0;a.ma[1][1]=1; b=MPow(a,m-1); /*for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ cout<<b.ma[i][j]<<" "; } cout<<endl; }*/ ll ans=(b.ma[0][0]*x+b.ma[0][1]*x)%mod; ///cout<<ans<<endl; if(ans%mod==c) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0;}
0 0
- 2016百度之星 hdu 5690 矩阵快速幂
- hdu5690百度之星All X(矩阵快速幂)
- 【矩阵快速幂】[2016"百度之星" - 初赛(Astar Round2A)]All X
- hdu 5690 矩阵快速幂/循环节
- HDU 5690 All X 矩阵快速幂
- 【矩阵快速幂】hdu 1575
- 【矩阵快速幂】hdu 1757
- hdu 2604 矩阵快速幂
- hdu-1575矩阵快速幂
- hdu 1575 矩阵快速幂
- hdu 1575(矩阵快速幂)
- hdu 3306 矩阵快速幂
- hdu 1757 矩阵快速幂
- hdu 1575 矩阵快速幂
- hdu 2855 矩阵快速幂
- hdu 3117 矩阵快速幂
- hdu 1005 矩阵快速幂
- hdu 4965 矩阵快速幂
- windows系统下载安装Redmine
- 数据结构课后题目源码
- 有了这个列表,程序员不愁没练手的小项目了
- Java读取配置文件
- 隐式Intent用法
- 2016百度之星 hdu 5690 矩阵快速幂
- Android 动态创建控件
- Leetcode 83. Remove Duplicates from Sorted List
- python中内置函数locals()和gloabls()解析
- Astar Round2A
- Genymotion如何启动网络连接
- web测试方法总结
- [JZOJ3397]雨天的尾巴
- SQL Server 2008 R2安装与使用