HDU 1852 Beijing 2008 数论

来源:互联网 发布:vb中str是什么意思 编辑:程序博客网 时间:2024/05/17 08:16

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1852


这道题和HDU1452类似。

题意:给你一个n、k,让你求2008^n所有因子的和(包括1和本身)%k,得到m,然后输出2008^m%k。

题解:看我HDU1452题,这里有一点需要注意的是:

s=(2^(3n+1)-1)(251^(n+1)-1)/250

因为gcd(250,k)不一定等于1,所以不能用求逆元的方法求解,

而k很小,所以我们可以将k乘以250,然后在进行,最后结果一定可以整除250.

(t/250)%k=(t%(250*k))/250


AC代码:

#include <iostream>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>using namespace std;typedef long long LL;const int N=52;const LL II=29;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);LL love(LL a,LL b,LL c){    LL ans=1;    while(b)    {        if(b&1) ans=(ans*a)%c;        a=(a*a)%c;        b=b>>1;    }    return ans%c;}int main(){    int i,j,T;    LL n,k,t;    while(scanf("%I64d%I64d",&n,&k)&&(n+k))    {        k=k*250;        t=(love(2,3*n+1,k)-1)*(love(251,n+1,k)-1);        t=(t%k+k)%k/250;        k/=250;        printf("%I64d\n",love(2008,t,k));    }    return 0;}/*1 100000 0*/


原创粉丝点击