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. JDK(Windows)

    百度云:链接:http://pan.baidu.com/s/1dEEsIUd     密码:15cn 官网下载网址:http://www.oracle.com/technetwork/java/jav ...

  2. [原创]一款基于Reactor线程模型的java网络爬虫框架

    AJSprider 概述 AJSprider是笔者基于Reactor线程模式+Jsoup+HttpClient封装的一款轻量级java多线程网络爬虫框架,简单上手,小白也能玩爬虫, 使用本框架,只需要 ...

  3. Metrics类型

    Metrics类型 在上一小节中我们带领读者了解了Prometheus的底层数据模型,在Prometheus的存储实现上所有的监控样本都是以time-series的形式保存在Prometheus内存的 ...

  4. Android 开发使用自定义字体

    有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在 assets 目录下. 自定义字体文件不能使用xml代码读取而应该使用java代码: publi ...

  5. Appium+python自动化(二十六)- 烟花一瞬,昙花一现 -Toats提示(超详解)

    简介 今天宏哥在这里首先给小伙伴们和童鞋们分享一个有关昙花的小典故:话说昙花原是一位花神,她每天都开花,四季都灿烂.她还爱上了每天给她浇水除草的年轻人.后来,此事给玉帝得知.于是,玉帝大发雷霆,要拆散 ...

  6. logging模块 旗舰版

    函数式简单配置 import logging logging.debug('debug message') logging.info('info message') logging.warning(' ...

  7. Where is the clone one and how to extract it?

    One cannot be in two places at once. Do you know what's "Dual Apps"? Manufactures like Xia ...

  8. AI and Robot

    Have you ever seen a movie called "Ex Machina"?  I like this movie very much. Artificial i ...

  9. dubbo是如何控制并发数和限流的?

    ExecuteLimitFilter ExecuteLimitFilter ,在服务提供者,通过 的 "executes" 统一配置项开启: 表示每服务的每方法最大可并行执行请求数 ...

  10. WPF后台设置颜色字体等

    Button TempButton = new Button();                                                TempButton.Tag = “按 ...