Dashing thro’ the snow,
In a one-horse open sleigh,
O’er the fields we go,
Laughing all the way;
Bells on bobtail ring,
Making spirits bright;
What fun it is to ride and sing
A sleighing song tonight!
Jingle, bells! Jingle, bells!
Jingle all the way!
Oh! What fun it is to ride in a one-horse open sleigh!

The Christmas Eve last year becomes a story that filled Christopher’s hearts with joy. With your help on “Souvenirs of Love” problem, Christopher has already been out of danger. (If you’re interested in the background, please see Christopher’s Key Ring)

This time, Christopher received a letter from Father Christmas. It was sent on Christmas Eve, but coz the mailing jam, it was just received. To his surprise, Father Christmas didn’t fall asleep as well as Christopher (but different reason ;). What is Santa Claus’s trouble? Let’s see, it’s a math problem:

There’re N different XX toys, to be sent to city X. Each time Santa Claus will send M (0 <= M <= N) toys to a single person as presents. The number of ways for fixed M is well-known I think. It is very strange that the people in city X hate such number M for a fixed N, if and only if the number of ways for it is multiple of a fixed prime P.

Santa Claus wonders how many Ms will be satisfying (not be hated by people in city X).

Input

The first line of the input contains one integer X (1 <= X <= 500), which is the number of test cases.

Next X lines each contains two integers N (1 <= N <= 10^100) and P (a prime, 2 <= P <= 10^7).

Output

You should write exactly X lines to the output. Each line contains the total number of satisfying Ms for that test case.

Sample Input

2
6 7
6 2

Sample Output

0
3

(adviser)
Site: http://zhuzeyuan.hp.infoseek.co.jp/index.files/our_contest_20040619.htm

问题问得就是m在0到n有多少个m使得Cmn% p≡0Cnm%p≡0C_n^m\%\ p\equiv0

这个只要一些Luas定理一个性质。
我们把n和m都按p进制表示:

n=n0+n1p+n2p2+...+nkpkm=m0+m1p+m2p2+...+mkpkn=n0+n1p+n2p2+...+nkpkm=m0+m1p+m2p2+...+mkpk

n=n_0+n_1p+n_2p^2+...+n_kp^k\\m=m_0+m_1p+m_2p^2+...+m_kp^k
那么

Cmn% p≡∏i=0kCmini% pCnm%p≡∏i=0kCnimi%p

C_n^m\%\ p\equiv\prod_{i=0}^kC_{n_i}^{m_i}\%\ p

若存在任意一项ni<mini<min_i,则这个组合数与0同余。
所以我们可以反过来看使得这个组合数不为0的时候m的情况,那么就是∏ki=0(ni+1)∏i=0k(ni+1)\prod_{i=0}^k(n_i+1)
答案就是n+1减掉这个不合法的。

java大数写的爽啊。
代码:

import java.math.BigInteger;
import java.util.Scanner;public class Main
{public static void main(String[] args){Scanner sc=new Scanner(System.in);int t=sc.nextInt();while((t--)>0){BigInteger n=sc.nextBigInteger();BigInteger p=sc.nextBigInteger();int cnt=0;BigInteger ans=n;BigInteger temp=BigInteger.ONE;while(!n.equals(BigInteger.ZERO)){temp=temp.multiply(n.mod(p).add(BigInteger.ONE));n=n.divide(p);}System.out.println(ans.subtract(temp).add(BigInteger.ONE));}}}

【ZOJ - 2116】Christopher's Christmas Letter(Lucas)相关推荐

  1. 【zoj 3547】【zoj 3548】8.17模拟赛

    题目来源:未知,zoj3547,3548 t1 [问题描述] 若干年之后,XJH找工作面试,面试官给了他这样一个题目. 你有一个N*N的回型矩阵,保证N是奇数. 例如,当N=5的时候,矩阵如下: 现在 ...

  2. 【ZOJ 4062】Plants vs. Zombies

    [链接] 我是链接,点我呀:) [题意] [题解] 二分最后的最大抵御值mid. 然后对于每个蘑菇. 都能算出来它要浇水几次mid/ai 然后如果第i个蘑菇没浇水达到要求次数. 就在i和i+1之间来回 ...

  3. 【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

    题干: Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used f ...

  4. 【ZOJ 2974】Just Pour the Water(矩阵快速幂)

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974 题意 给出n个杯子与初始水量同时进行操作 将其中的水同时平均 ...

  5. 【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)

    题干: There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and D ...

  6. 【ZOJ - 2968 】Difference Game (贪心,思维模拟)

    题干: Now you are going to play an interesting game. In this game, you are given two groups of distinc ...

  7. *【ZOJ - 3703】Happy Programming Contest(带优先级的01背包)

    题干: In Zhejiang University Programming Contest, a team is called "couple team" if it consi ...

  8. 【ZOJ - 4033】CONTINUE...? (思维,整体思想,分组思想)

    题干: DreamGrid has  classmates numbered from  to . Some of them are boys and the others are girls. Ea ...

  9. 【ZOJ - 4032】Magic Points (思维,几何,构造)

    题干: 解题报告: 想到了,这样绕圈构造.但是这样有个问题,最后一个点如何构造. 刚开始想的是n=奇数  ,  就8 10 这样的连一条,n=偶数  就8 11 这样的连一条,随便构造一下就行,但是发 ...

最新文章

  1. Collecting package metadata (current_repodata.json): done Solving environment: failed with repodata
  2. java安全编码指南之:序列化Serialization
  3. Flume自定义Hbase Sink的EventSerializer序列化类
  4. 嵌入式linux的调试技术
  5. NEC向格鲁吉亚提供基于面部识别技术的城市监控系统
  6. SpringBoot 如何防御 CSRF 攻击?
  7. zedboard的DDR3型号MT41K128M16HA
  8. linux光盘挂载加载过程,如何在Linux系统下挂载光盘
  9. 最新黑马软件测试全套视频教程
  10. OBJ格式建筑模型数据处理步骤
  11. ​LeetCode刷题实战592:分数加减运算
  12. C#反射基础知识实例总结
  13. 小白应该如何选择国内服务器?腾讯云、阿里云、小鸟云?
  14. 【卡夫卡的岛上书店】:一个利用 vuepress 的主题 vuepress-theme-reco 以及 vuepress-theme-vdoing 搭建自己的静态博客
  15. #JAVA# JAVA简易版计算器GUI编程练习
  16. 跑步机健身器材标准ENISO20957检测范围有哪些
  17. DSP TMS320F28377D与TMS320F28335硬件资源对比
  18. 微信插件第一讲之自动回复机器人(护妻宝)
  19. oracle之汉字转拼音
  20. 消费信贷业务风控英文词汇手册

热门文章

  1. 视频监控在油气长输管道巡护管理的应用解决方案
  2. php将数组变成字符串implode和explode
  3. 【AUTOSAR-COM】-9.1-Tx/Rx Ipdu两种处理模式:IMMEDIATE和DEFERRED详解
  4. OSChina 周三乱弹 —— 写得了代码,查得出异常
  5. 6.20校内考试整理——大美江湖腐草为萤锦鲤抄题解
  6. 《Comprehensive Privacy Analysis of Deep Learning》补完以及Deepfool对抗样本构造算法
  7. 今日头条的文章怎么同步到其他自媒体平台?
  8. 优酷、YouTube、Twitter及JustinTV几个视频网站的架构
  9. mysql 强制使用索引 FORCE INDEX(idx_name)
  10. 软件测试2020年终总结