您的位置:首页 > 其它

2016百度之星 hdu 5690 矩阵快速幂

2016-05-23 20:44 519 查看
链接:戳这里

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)-1
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: