栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3707 Calculate Prime S

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3707 Calculate Prime S

#include <iostream>#include <cstdio>#include <cstring>#include <string>#define LL long long#define maxn 16000000using namespace std;LL mod;int x, m, k;LL p[maxn];bool prime[maxn];int cnt=1;struct Matrix{ LL ma[2][2]; Matrix operator*(const Matrix a) { Matrix ans; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ans.ma[i][j]=0; for (int k = 0; k < 2; k++) { ans.ma[i][j]+= ((ma[i][k]*a.ma[k][j])%mod); ans.ma[i][j] %= mod; } } } return ans; }};Matrix a = {1,1,1,0};void isprime(){ memset(prime, true, sizeof(prime)); for (int i = 2; i <= maxn; i++) { if(prime[i]) { p[cnt++]=i; for (int j = 2*i; j < maxn; j+=i) { prime[j] = false; } } } p[1]=3;p[2]=4;}Matrix mul2(LL k){ Matrix ans = a, t = a; while(k) { if(k&1) ans = ans * t; k>>=1; t = t * t; } return ans;}int main(){ isprime(); int ncase, t; Matrix ans; scanf("%d",&ncase); while(ncase--) { scanf("%d%d%d",&k,&x,&m); t = p[k]; mod = m * x; while(1) { ans = mul2(t); if(ans.ma[1][1]%x==0) { break; } t++; } ans = mul2(t); cout << ans.ma[1][1]/x <<endl; } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378711.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号