hdu2837(2009多校第三场) a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)

公式如上,dfs函数思考清楚==

另外欧拉函数以前写的可以换了

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #define LL long long
 5 LL phi(LL x)
 6 {
 7   LL ans=x,i;
 8   for (i=2;i*i<=x;i++)
 9     if  (x%i==0)
10     {
11       ans=ans/i*(i-1);
12       while (x%i==0) x/=i;
13     }
14   if (x!=1) ans=ans/x*(x-1);
15   return ans;
16 }
17 LL quick(LL a,LL b,LL m)
18 {
19   LL ans=1;
20   while (b!=0)
21   {
22     if (b%2) ans=ans*a%m;
23     b/=2;
24     a=a*a%m;
25   }
26   return ans;
27 }
28 LL check(LL a,LL b,LL m)
29 {
30   LL ans=1,i;
31   for (i=1;i<=b;i++)
32   {
33     ans=ans*a;
34     if (ans>=m) return ans;
35   }
36   return ans;
37 }
38 LL dfs(LL n,LL m)
39 {
40   if (n<10) return n;
41   LL p,tmp,y,ans;
42   p=phi(m);
43   tmp=dfs(n/10,p);
44   y=check(n%10,tmp,m);
45   if (y>=m) {
46     ans=quick(n%10,tmp+p,m); 
47     return ans==0?m:ans;
48   }
49   return y;
50 }
51 int main()
52 {
53   LL T,n,m;
54   scanf("%I64d",&T);
55   while (T--)
56   {
57     scanf("%I64d%I64d",&n,&m);
58     printf("%I64d\n",dfs(n,m)%m);
59   }
60   return 0;
61 }

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2837

转载于:https://www.cnblogs.com/xiao-xin/articles/4156696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值