Beijing 2008

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

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
 
收获挺大的!。以前对于除法模运算只知道用逆元可以算,,但是当两个数不互素的时候就不知道怎么弄了。今天得到了两个公式。。第一个公式自己做的时候想到了可能可以,然后真的AC了,然后去验证发现真的有:
1.(a/b)%mod=a%(b*mod)/b%mod;(get这个公式好激动)

2.(a/b)%mod=a*b^(mod-2)%mod,mod为素数(可以通过逆元证明)(这个公式的话感觉如果mod为素数的话,直接用逆元也一样的,,可以参考我博客hdu1452)

然后这个题并不难,把2008分解成 251*2^3 然后求因子和用第一个公式去掉分母250,然后可以得到M,在用快速幂计算就好了。

#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long LL; LL pow_mod(LL a,LL n,LL mod){
LL ans = ;
while(n){
if(n&) ans = a*ans%mod;
a=a*a%mod;
n=n>>;
}
return ans;
} int main()
{
LL N,K;
while(scanf("%lld%lld",&N,&K)!=EOF,N&&K)
{
K = *K;
LL M = ((pow_mod(,N+,K)-)*(pow_mod(,*N+,K)-))%K;
M = M/;
K/=;
LL ans =pow_mod(,M,K);
printf("%lld\n",ans);
}
return ;
}

hdu 1852(快速幂模+有除法的时候取模的公式)的更多相关文章

  1. # 模乘(解决乘法取模爆long long)

    模乘(解决乘法取模爆long long) 二进制思想,变乘法为多次加法,具体思想跟着代码手算一遍就理解了,挺简单的 ll qmul(ll a,ll b,ll m) { ll ans=0; while( ...

  2. hdu 5187 快速幂 + 快速乘 值得学习

    就是以那个ai为分水岭,左边和右边都分别是单调增或单调减如图         就这四种情况,其中头两种总共就是两个序列,也就是从头到尾递增和从头到尾递减.         后两种方式就是把序列中德数分 ...

  3. hdu 1061 快速幂

    求n^n的个位 Sample Input 2 3 4 Sample Output 7 6 直接快速幂了,注意要用long long #include<cstdio> long long q ...

  4. C - Rikka with Badminton --- HDU 6425 快速幂加组合数学

    ---恢复内容开始--- 题目链接: https://vjudge.net/problem/1812693/origin 这题的mod运算很恶心,真的... 本题有正反两个思路,一个是正面求解其不能成 ...

  5. 本原串(HDU 2197 快速幂)

    本原串 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. hdu 4506 快速幂

    小明自从告别了ACM/ICPC之后,就开始潜心研究数学问题了,一则可以为接下来的考研做准备,再者可以借此机会帮助一些同学,尤其是漂亮的师妹.这不,班里唯一的女生又拿一道数学题来请教小明,小明当然很高兴 ...

  7. HDU 1097 快速幂

    #include<iostream> using namespace std; long long quick(long long a,long long b,int c) { ; a=a ...

  8. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  9. 乘方快速幂 OR 乘法快速幂

    关于快速幂这个算法,已经不想多说,很早也就会了这个算法,但是原来一直靠着模板云里雾里的,最近重新学习,发现忽视了一个重要的问题,就是若取模的数大于int型,即若为__int64的时候应该怎么办,这样就 ...

随机推荐

  1. 3分钟快速了解FastDFS

    1.介绍 FastDFS是一个C语言写的阿里开源的分布式文件存储服务器主要由两部分组成:1.Tracker server ——————主要负责调度和追踪Storage状态(调度服务器),默认监听端口: ...

  2. vector函数用法

    vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问元素,还可以使用指针的 ...

  3. Maya建模命令

    Surface-Loft(放样)在两条曲线中间生成曲面Section Radius 改变圆环的圆环半径Edit Mesh- Merge 点连结挤压 keep face together(整体挤压),若 ...

  4. 商品评分效果JavaScript

    <script> window.onload=function(){ //----------选中的星星会多出一个属性:isClick="true" 藉此来获取评分-- ...

  5. LOJ #6008. 「网络流 24 题」餐巾计划

    #6008. 「网络流 24 题」餐巾计划 题目描述 一个餐厅在相继的 n nn 天里,每天需用的餐巾数不尽相同.假设第 i ii 天需要 ri r_ir​i​​ 块餐巾.餐厅可以购买新的餐巾,每块餐 ...

  6. Java-读取txt生成excel

    本段代码的目的是从txt文本中读取相应格式的数据,然后写入到对应格式的excel文档中 在敲本段代码的时候,也学习了一些其它知识点,如下: 1.byte[] b_charset= String.get ...

  7. luogu2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  8. 二叉树遍历(Java实现)

    二叉树遍历(Java实现)   主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...

  9. IOS开发---菜鸟学习之路--(五)-MacBook购买前后感想

    前几天刚入手了一台MACBOOK AIR 13寸 13版的 这几天使用过来个人感觉还是非常不错的. 这几天每天晚上都抱着她玩到十一.二点. 今天晚上突然想起来好久没续写博客了.就连忙开始码字了. 此章 ...

  10. Python学习-day13 SqlAlchemy

    本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM介绍 orm英文全称object relational mapping ...