hdu 1852 Beijing 2008(快速幂取模)

本文介绍了一种高效的算法来解决特定形式的大数模运算问题,即计算2008的n次方对某个较大数k取模的结果,并进一步计算2008对该结果取模的值。通过使用快速幂运算技巧和巧妙的数学转换,避免了直接计算带来的巨大开销。

Beijing 2008

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 744    Accepted Submission(s): 294


Problem Description
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a little special somehow. You are looking forward to it, too, aren't you? Unfortunately there still are months to go. Take it easy. Luckily you meet me. I have a problem for you to solve. Enjoy your time.

Now given a positive integer N, get the sum S of all positive integer divisors of 2008 N. Oh no, the result may be much larger than you can think. But it is OK to determine the rest of the division of S by K. The result is kept as M.

Pay attention! M is not the answer we want. If you can get 2008 M, that will be wonderful. If it is larger than K, leave it modulo K to the output. See the example for N = 1,K = 10000: The positive integer divisors of 20081 are 1、2、4、8、251、502、1004、2008,S = 3780, M = 3780, 2008 M % K = 5776. 

 

Input
The input consists of several test cases. Each test case contains a line with two integers N and K (1 ≤ N ≤ 10000000, 500 ≤ K ≤ 10000). N = K = 0 ends the input file and should not be processed.
 

Output
For each test case, in a separate line, please output the result.
 

Sample Input
  
1 10000 0 0
 

Sample Output
  
5776
题意:给你n和k,2008的n次方对k取余为m,求2008的m次方对k取余

思路:不要用库函数的pow很慢,用快速幂,只要log(n)的时间。

另外,这道题不能用乘法逆元去处理k,因为乘法逆元要求必须互素。

所以我们可以运用这个公式

x/d%k==x%(d*k)/d这样就可以直接得到答案了。注意取模就要全部都对(d*k)取了~

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL long long
LL pow_mod(LL a,LL n,int k)
{
    LL ans=1;
    while(n)
    {
        if(n&1) ans=ans*a%k;
        a=a*a%k;
        n>>=1;
    }
    return ans;
}
int main()
{
    int n,k;
    while(scanf("%d %d",&n,&k)&&(n+k))
    {
        LL ans=1;
        LL temp=pow_mod(2,3*n+1,250*k)-1;
        LL temp1=pow_mod(251,n+1,250*k)-1;
        ans=temp*temp1%(250*k)/250;
        ans=pow_mod(2008,ans,k);
        printf("%lld\n",ans);
    }
    return 0;
}


HDU 2008 是杭州电子科技大学(HDUACM 在线评测系统中的一道题目,名为“数值统计”。 ### 题目大意 该题主要是将输入的数据分为三类,即大于零、等于零、小于零,然后分别统计这三类数据的数量并输出。 ### 注意事项 在处理该问题时,需要注意输入数据的类型,因为输入数据可能包含小数,所以要用`double`类型输入数据,若一直用`int`类型,当输入如 3.5 等小数时就会出错 [^4]。 ### 代码示例 以下是几种不同的 AC 代码实现: #### C++ 代码 ```cpp #include <iostream> using namespace std; int main() { int n; float temp; while (cin >> n && n) { int a = 0, b = 0, c = 0; for(int i = 0; i < n; i++) { cin >> temp; if(temp == 0) b++; else if(temp > 0) c++; else a++; } cout << a << " " << b << " " << c << endl; } return 0; } ``` #### C 语言代码 1 ```c #include <stdio.h> int main() { int n,a,b,c; while (scanf("%d", &n) != EOF&&n) { a = b = c = 0; double x; while (n--&&scanf("%lf", &x) != EOF) { if (x < 0) a++; else if (x == 0) b++; else c++; } printf("%d %d %d\n", a, b, c); } return 0; } ``` #### C 语言代码 2 ```c #include <stdio.h> int main(void) { int n, i, a, b, c; double x; while (scanf("%d", &n) , n) { a = b = c = 0; for (i = 0 ; i < n ; i++) { scanf("%lf", &x); if (x > 0) c++; else if (x < 0) a++; else b++; } printf("%d %d %d\n", a, b, c); } return 0; } ``` #### C 语言代码 3 ```c #include <stdio.h> void main() { int n,i=0,a,b,c; double m; while(scanf("%d",&n)!=EOF) { if(n==0) break; a=0; b=0; c=0; for(i=0;i<n;i++) { scanf("%lf",&m); if(m<0) a++; else if(m==0) b++; else c++; } printf("%d %d %d\n",a,b,c); } } ``` #### C 语言代码 4 ```c #include <stdio.h> int main() { int n; while(scanf("%d",&n)!=EOF&&(n!=0)) { int a=0,b=0,c=0; for(int i=1;i<=n;i++) { double t; scanf("%lf",&t); if(t<0) a+=1; if(t==0) b+=1; if(t>0) c+=1; } printf("%d %d %d\n",a,b,c); } return 0; } ``` ### 题目链接 题目连接:[http://acm.hdu.edu.cn/showproblem.php?pid=2008](http://acm.hdu.edu.cn/showproblem.php?pid=2008) [^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值