Gerg's Cake

Gerg is having a party, and he has invited his friends. p of them have arrived already, but a are running
late. To occupy his guests, he tried playing some team games with them, but he found that it was
impossible to divide the p guests into any number of equal-sized groups of more than one person.
Luckily, he has a backup plan | a cake that he would like to share between his friends. The cake is
in the shape of a square, and Gerg insists on cutting it up into equal-sized square pieces. He wants to
reserve one slice for each of the a missing friends, and the rest of the slices have to be divided evenly
between the p remaining guests. He does not want any cake himself. Can he do it?
Input
The input will consist of several test cases. Each test case will be given as a non-negative integer a and
a positive integer p as specied above, on a line. Both a and p will t into a 32-bit signed integer. The
last line will contain `-1 -1' and should not be processed.
Output
For each test case, output `Yes' if the cake can be fairly divided and `No' otherwise.
Sample Input
1 3
1024 17
2 101
0 1
-1 -1
Sample Output
Yes
Yes
No
Yes
题意:输入a,p,问把一个正方形蛋糕分成若干个小正方形,然后分给还没到的a个人一人一个后再使得已经到的p个人正好平分。
分析:本题容易被样例误导,以为只要a+p是素数就输出“Yes”。实际上并不是这么一个回事。
假设切完之后的蛋糕有x*x个,那么就有x*x=a+n*p,其中n是一个整数。
我们对这个式子左右两边同时求余就有:x*x = a (mod p)。
联系费马小定理:x^(p-1) =1 (mod p).
就有:x^(p-1)=a^(p-1)/2=1   (mod p)
现在只要计算:a^(p-1)/2 =1 (mod p)是否成立即可。
实现过程写一个快速幂就行了,不过要注意的是底数a进行求幂运算之前应该先进行a%p否则有可能会因为溢出导致WA
 
AC code:
#include<bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
ull qp(ull a,ull b,ull p)
{
ull ans=;
while(b)
{
if(b&) ans=(ans*a)%p;
a=(a*a)%p;
b>>=;
}
return ans%p;
}
int main()
{
freopen("input.txt","r",stdin);
ull a,p;
while(~scanf("%llu%llu",&a,&p))
{
if(a==-&&p==-) break;
a%=p;
if(a==) printf("Yes\n");
else
{
if(qp(a,(p-)/,p)==) printf("Yes\n");
else printf("No\n");
}
}
return ;
}

UVA10831题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. 机器学习-特征值,svd分解

    求矩阵的秩 设 ,已知r(A)=2,则参数x,y分别是 解:任意三阶子式=0,有二阶子式≠0,但是这些子式比较多,可以使用初等变换,因为初等变换不改变矩阵的秩,可以将矩阵通过初等行(列)变换,化为行阶 ...

  2. Jquery第一次考核

    1. 什么是JS JavaScript 缩写.一种计算机脚本语言 JavaScript是一种动态.弱类型.基于原型的语言,通过浏览器可以直接执行 2. JS三大组成部件 ECMAScript DOM ...

  3. JS原生隐士标签扩展

    最近项目开发中,开发了不少的接口,有一个接口是这样子的.先从A公司拿到数据后,存放到我们公司数据库里,然后需要将数据展示给客户,下面这个界面,后台要实时刷新,后台写了个定时器,2S刷一次从后台拼接好H ...

  4. 面试必问之ArrayList

    ArrayList概述 (1)ArrayList 是一种变长的集合类,基于定长数组实现. (2)ArrayList 允许空值和重复元素,当往 ArrayList 中添加的元素数量大于其底层数组容量时, ...

  5. http://regex.alf.nu/ 非标准答案

    Plain strings (207)                             foo Anchors (206)                                   ...

  6. Qtech 暑假未讲到的算法(不完全)

    一.数据结构:    优先队列.堆.RMQ问题(区间最值问题,可以用线段树解决,还有一个Sparse-Table算法).排序二叉树.划分树.归并树.....   字符串处理:    KMP.字典树.后 ...

  7. 从windows平台转战ubuntu

    说到ubuntu,可能很多人会有些陌生,但对于有些人很熟悉.ubuntu是linux里面最为流行的一版,以下来自百度百科.       Ubuntu(乌班图)是基于Debian GNU/Linux,支 ...

  8. UE4 打包详细流程

    这两天试着把之前做的一个UE4项目在安卓机上运行下,于是乎有了下面的一个打包血泪史. 首先呢,肯定是下载好了UE的源码了,我用的是4.18. 安装步骤可以先参考下官方的教程http://api.unr ...

  9. jenkins弱口令漏洞

    jenkins弱口令漏洞 一.漏洞描述 通过暴力破解管理控制台,如果爆破成功,可获得后台管理权限.操作后台,后台可通过脚本命令行功能执行系统命令,如反弹shell等,低权限可以通过创建控制台输出方式执 ...

  10. HttpsUtils

    package io.renren.modules.jqr.util; import java.io.BufferedReader; import java.io.InputStream; impor ...