• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

会点儿code

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

HDU1852 (Beijing 2008)

                          Beijing 2008

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

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 2008N. 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 2008M, 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, 2008M % 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

// 这题主要求S
// 结论: S = (251^(n+1)-1) * (2^(3n+1)-1) / 250 
// 是两个等比数列和相乘 
// 
// 推理:
// 2008 = 2^3 * 251 
// 所以 2008^N 有 3N 个 2 和 N 个251 
// 所有仅由2组成的因子有
// 2^0 2^1 2^2 ... 2^(3N)
// 设集合 C = {2^0, 2^1, 2^2 ...,2^(3N)};
// SUM(C) =  2^(3n+1)-1

// 跟251组合产生的因子有
// 251^0 * C
// 251^1 * C
// ...
// 251^N * C

// 所有因子和为:
// S = (251^(n+1)-1))/250 * (2^(3n+1)-1)

// 计算S%K:
// S 很大, 不能保存在普通的数据类型中, 需要直接计算S%K
// 因为S有个分母250, 设 S = X/250
// 则S%K = (X/250)%K = (X%(250*K))/250
// 变成先求余数再除法的形式

#include <stdio.h>

// 求 (x^exp)%p 的值  O(LOG(exp))
int ExpMod(int x, int exp, int p) {
     if( exp==0 ) {
          return 1;
     }
     long long ret = ExpMod(x, exp>>1, p);
     ret = (ret*ret)%p;
     if( exp & 1 ) {
          ret = (ret*x)%p;
     }
     return (int)ret;
}

int main() {
     int N, K;
     while( scanf("%d %d", &N, &K), N && K ) {
          long long M = ExpMod(251, N+1, 250*K);
          M = (M+250*K-1)%(250*K);          // M--;

          long long M2 = ExpMod(2, 3*N+1, 250*K);
          M2 = (M2+250*K-1)%(250*K);          // M--;

          M = (M*M2)%(250*K);
          M /= 250;

          printf("%d\n", ExpMod(2008, M, K));
     }

     return 0;
}

posted on 2008-04-21 18:32  曹某  阅读(438)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3