Happy Equation
Source: The 10th Shandong Provincial Collegiate Programming Contest
题解:
因为2^p为偶数,所以a,x的奇偶性相同
1.当a为奇数时,答案始终为1
2.下面讨论a为偶数的情况
因为a为偶数,所以a=2*other,a^x=2^x*other^x;
当x>=p时,a^x%2^p==0,此时只需要关心x^a%2^p==0,即x^a是2^p的倍数;
因为x为偶数,所以x=2^q*tmp,x^a=(2^q)^a*tmp^a=2^qa*tmp^a;
另tmp==1,并让2^qa>=2^p,即qa>=p,解得q>=p/a向上取整,满足x^a是2^p的倍数的q的最小值(也就是说x中必须要包含一个2^q才能是2^p的倍数);
由于1<=x<=2^p,故ans+=(2^p)/(2^q)-(p-1)/(2^q);
当x<p时,可以直接暴力
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
ll a,p,mod,ans;
ll qpow(ll x,ll y)
{
ll res=;
while(y){
if(y%==) res=res*x%mod;
x=x*x%mod;
y>>=;
}
return res;
}
int main()
{
int t;
for(scanf("%d",&t);t;t--)
{
mod=;ans=;
scanf("%lld %lld",&a,&p);
if(a%==)
{
printf("1\n");
continue;
}
mod<<=p;
for(ll i=;i<p;i+=)
{
if(qpow(i,a)==qpow(a,i)) ans++;
}
ll q=(p-)/a+;
ll cnt=;cnt<<=q;
ans+=mod/cnt-(p-)/cnt;
printf("%lld\n",ans);
}
return ;
}
Happy Equation的更多相关文章
- CodeForces460B. Little Dima and Equation
B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...
- ACM: FZU 2102 Solve equation - 手速题
FZU 2102 Solve equation Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- HDU 5937 Equation
题意: 有1~9数字各有a1, a2, -, a9个, 有无穷多的+和=. 问只用这些数字, 最多能组成多少个不同的等式x+y=z, 其中x,y,z∈[1,9]. 等式中只要有一个数字不一样 就是不一 ...
- coursera机器学习笔记-多元线性回归,normal equation
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- CF460B Little Dima and Equation (水题?
Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...
- Linear regression with multiple variables(多特征的线型回归)算法实例_梯度下降解法(Gradient DesentMulti)以及正规方程解法(Normal Equation)
,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, , ...
- ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分
Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- [ACM_数学] Counting Solutions to an Integral Equation (x+2y+2z=n 组合种类)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27938#problem/E 题目大意:Given, n, count the numbe ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- SGU 106 The equation
H - The equation Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Subm ...
随机推荐
- 大白话5分钟带你走进人工智能-第二十六节决策树系列之Cart回归树及其参数(5)
第二十六节决策树系列之Cart回归树及其参数(5) 上一节我们讲了不同的决策树对应的计算纯度的计算方法, ...
- [51nod] 1007 正整数分组 dp
将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个数N,N为正整数的数量 ...
- String,StringBuffer和StringBuilder
String,StringBuffer和StringBuilder分别应该在什么情况下使用? String 是Java的字符串类,其实质上也是用Char类型存储的,但是除了hash属性,其他的属性都声 ...
- 初探 Nginx 架构
转载自:http://wiki.jikexueyuan.com/project/nginx/nginx-framework.html Nginx 在启动后,在 unix 系统中会以 daemon 的方 ...
- 黑马旅游网 解析url查询字符串
function getUrlParam(name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)( ...
- SQL SERVER Study
1. SQL SERVER Install 可以免费试用天.最新的是2014版本. 2. Attach, Detach 可以添加或去掉已有的数据库文件. 数据库文件的格式是: .md ...
- Spring @Import 注解
@Import 导入某个bean 文件 @Configuration @Import({User.class,MyImportSelector.class,MyImportBeanDefinitio ...
- 根据经纬度反向地理编译出地址信息(如果报错:Error Domain=kCLErrorDomain Code=8 "(null)")
注意:Error Domain=kCLErrorDomain Code=8 "(null)" 如果出现这个错误 一定是 经纬度有问题 一定是 经纬度有问题 一定是 经纬度有问 ...
- BZOJ-3555:企鹅QQ(字符串哈希)
PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体验,满足用 ...
- CodeForces - 500A-New Year Transportation(模拟)
New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, ...