HDOJ3398

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

很好的题目,考查数学思想!

基本上是快速幕+  N!的快速质因数分解,

看了他人的解题报告:

我写的代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 const int max1=2000001;
 5 int pri[max1];
 6 int b[max1];
 7 int ci[max1];
 8 int t=0;
 9 void prime()
10 {
11     for (int i=4;i<=max1;i+=2)
12     b[i]=1;
13     for (int i=3;i<=sqrt(max1);i+=2)
14         if (b[i]==0)
15         {
16         for (int j=i*i;j<=max1;j+=i)
17             b[j]=1;
18        }
19       for (int i=2;i<=max1;i++)
20         if (b[i]==0) pri[++t]=i;
21 }
22 long long mulpow(long long a,int b,int k)
23   {
24       long long x=1;
25    while (b)
26    {
27        if (b&1) x=x*a%k;
28        a=a*a%k;
29        b/=2;
30    }
31    return x;
32 }
33 int  gets(int n,int b)
34 {
35     int i;
36     for (i=1;i<=t&&pri[i]<=n;i++)
37     {
38      int x=n;
39      while (x)
40     {
41         ci[i]+=b*(x/pri[i]);
42         x/=pri[i];
43     }
44     }
45     return i;
46 }
47  int main()
48  {
49      prime();
50      int cas;
51      scanf("%d",&cas);
52      while (cas--)
53      {
54          int n,m;
55          memset(ci,0,sizeof(ci));
56          scanf("%d%d",&n,&m);
57          int kk=n+1-m;
58          for (int i=1;i<=t&&kk>=pri[i];i++)
59             while (kk%pri[i]==0)
60             {
61              ci[i]++;
62              kk/=pri[i];
63          }
64         int l=gets(n+m,1);
65          gets(n+1,-1);
66          gets(m,-1);
67          long long ans=1;
68          int tt=20100501;
69          for (int i=1;i<=l;i++)
70             ans=ans*mulpow(pri[i],ci[i],tt)%tt;
71             printf("%I64d\n",ans);
72      }
73      return 0;
74  }

写的很好的BOLG:http://hi.baidu.com/matrush/item/e2a738d70f638b392a35c75c

posted on 2013-11-24 09:32  forgot93  阅读(127)  评论(0编辑  收藏  举报

导航