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

zoj 2964 Triangle

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

zoj 2964 Triangle

#include <iostream>#include <algorithm>#include <cmath>typedef long long LL;using namespace std;bool isPrime[60000];int prime[6100], primeCnt;int a, z;int p[50], c[50], pcnt;LL factor[30000];int fcnt;inline void init(){    for(int i = 2; i < 60000; ++i)        isPrime[i] = 1;    for(int i = 2; i < 250; ++i)        if(isPrime[i]) for(int j = i; i*j < 60000; ++j) isPrime[i*j] = 0;    primeCnt = 0;    for(int i = 2; i < 60000; ++i)        if(isPrime[i]) prime[primeCnt++] = i;}inline LL euler(int x){    LL y = 1;    for(int i = 0; x >= prime[i] && i < primeCnt; ++i)    {        if(x % prime[i] == 0)        { y *= prime[i] - 1; x /= prime[i]; while(x % prime[i] == 0) {     x /= prime[i];     y *= prime[i]; }        }    }    if(x != 1) y *= x - 1;    return y;}void DFS(int x, int s){    if(x == pcnt)    {        factor[fcnt++] = s;        return;    }    DFS(x+1, s);    for(int i = 1; i <= c[x]; ++i)    {        s *= p[x];        DFS(x+1, s);    }}LL check(LL x){    if(x == 1LL) return (LL)(a % z);    LL rmd = check(x/2)*check(x/2) % z;    if(x % 2 == 1) rmd = rmd * a % z;    return rmd;}int main(void){    init();    int T;    cin >> T;    while(T --)    {        cin >> a >> z;        if(a == 1 || z == 1)        { cout << "9n"; continue;        }        LL eu = euler(z);        pcnt = 0;        for(int i = 0; i < primeCnt; ++i)        { if(eu % prime[i] == 0) {     p[pcnt] = prime[i];     c[pcnt] = 0;     while(eu % prime[i] == 0)     {         eu /= prime[i];         ++ c[pcnt];     }     ++pcnt; }        }        if(eu != 1LL)        { p[pcnt] = eu; c[pcnt] = 1; ++ pcnt;        }        fcnt = 0;        DFS(0, 1);        sort(factor, factor+fcnt);        bool flag = 0;        for(int i = 0; i < fcnt-1; ++i)        { if(check(factor[i]) == 1LL) {     cout << factor[i]*6 + 3 << endl;     flag = 1;     break; }        }        if(flag == 0) cout << factor[fcnt-1]*6 + 3 << endl;    }}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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