[关闭]
@Chilling 2016-08-11T07:51:02.000000Z 字数 1537 阅读 794

Educational Codeforces-13D: Iterated Linear Function(费马小定理)

数论


Description

Consider a linear function . Let's define and for . For the given integer values and find the value of modulo .

Input

The only line contains four integers and — the parameters from the problem statement.

Note that the given value n can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Output

Print the only integer s — the value modulo .

Input

3 4 1 1
3 4 2 1
3 4 3 1

Output

7
25
79

题意:,给出A,B,n和x,求 的值。

分析:……写出来之后发现:




……
得到:


因此后面一部分为等比数列,可以用等比数列求和公式求值,公比为 A

可以看出,A 不能为1,所以要特判A为1时的情况
时,

又因为要模,等比公式求和的时候出现了分数,因此我们用到费马小定理
即为关于模mod的乘法逆元,就将除法转化为了乘法。


  1. #include<stdio.h>
  2. #define LL long long
  3. const LL MOD=1e9+7;
  4. LL quick(LL x,LL m,LL n)
  5. {
  6. LL ans=1;
  7. while(m>0)
  8. {
  9. if(m%2==1)
  10. ans=ans*x%n;
  11. x=x*x%n;
  12. m=m/2;
  13. }
  14. return ans;
  15. }
  16. int main()
  17. {
  18. LL a,b,n,x,s,ss,sss;
  19. LL zuo,you;
  20. while(scanf("%lld%lld%lld%lld",&a,&b,&n,&x)!=EOF)
  21. {
  22. if(a==1) //特判a为1的时候
  23. sss=(x%MOD+((b%MOD)*(n%MOD))%MOD)%MOD;
  24. else
  25. {
  26. s=quick(a,n,MOD); //a^n
  27. zuo=(s*x%MOD)%MOD; //x*a^n; //非等比数列部分
  28. ss=quick(a-1,MOD-2,MOD); //求a-1关于mod的乘法逆元
  29. you=(s-1%MOD+MOD)%MOD; //a^n -1 %MOD 分子部分
  30. you=you*ss%MOD*b%MOD;
  31. sss=(zuo+you)%MOD;
  32. }
  33. printf("%lld\n",sss);
  34. }
  35. return 0;
  36. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注