【BZOJ 3884】 上帝与集合的正确用法|欧拉函数
2016-04-18 17:26
495 查看
开始我感觉题目有问题 。。
后来问了下发现题解说的很对 http://blog.csdn.net/popoqqq/article/details/43951401
快速幂写错耽误一段时间.... 搞清幂和指数!注意LL!!!
(LL)(t*t)
这样会爆 T_T
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
int f(int y,int p)
{
if(y==0) return 1%p;
int t=f(y/2,p);
t=(LL)t*t%p;
if(y&1) t=(LL)t*2%p;
return t;
}
int get_phi(int x)
{
int ans=x;
for(int i=2;i*i<=x;i++)
if(x%i==0)
{
ans/=i;
ans*=i-1;
while(x%i==0) x/=i;
}
if(x!=1) ans/=x,ans*=x-1;
return ans;
}
int solve(int p)
{int xxx=p;
if(p==1) return 0;
int tmp=0;
while(p%2==0) p>>=1,tmp++;
int phi=get_phi(p);
int ans=solve(phi);
ans=((ans-tmp)%phi+phi)%phi;
ans=f(ans,p);
return ans<<tmp;
}
int main()
{
int t,x;
cin>>t;
while(t--)
{
cin>>x;
cout<<solve(x)<<endl;
}
}
后来问了下发现题解说的很对 http://blog.csdn.net/popoqqq/article/details/43951401
快速幂写错耽误一段时间.... 搞清幂和指数!注意LL!!!
(LL)(t*t)
这样会爆 T_T
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
int f(int y,int p)
{
if(y==0) return 1%p;
int t=f(y/2,p);
t=(LL)t*t%p;
if(y&1) t=(LL)t*2%p;
return t;
}
int get_phi(int x)
{
int ans=x;
for(int i=2;i*i<=x;i++)
if(x%i==0)
{
ans/=i;
ans*=i-1;
while(x%i==0) x/=i;
}
if(x!=1) ans/=x,ans*=x-1;
return ans;
}
int solve(int p)
{int xxx=p;
if(p==1) return 0;
int tmp=0;
while(p%2==0) p>>=1,tmp++;
int phi=get_phi(p);
int ans=solve(phi);
ans=((ans-tmp)%phi+phi)%phi;
ans=f(ans,p);
return ans<<tmp;
}
int main()
{
int t,x;
cin>>t;
while(t--)
{
cin>>x;
cout<<solve(x)<<endl;
}
}
相关文章推荐
- 一种写法简洁且高效的数组查找元素方法
- HDU1203 I NEED A OFFER! 01背包DP
- 简单讲解jQuery中的子元素过滤选择器
- Nagios利用NSClient++监控Windows主机
- Windows上安装Mac OS
- 如何让Oracle客户端显示中文
- eclipse 安装jetty WTP Adaptor插件
- spring mvc+Mybatis整合shiro 第四章 SessionDAO
- 七层网络模型(OSI)
- 总结
- [BS-00] const限定常量或者变量(初次赋值后),其值不允许被改变
- Linq-语句之存储过程
- OpenWRT使用SNMP监测网络状态
- HDU 5558 Alice's Classified Message 后缀自动机
- Elipse Logcat 只显示level问题
- MATLAB的size(),length(),ndims()函数
- opencl多核编程模型
- Linux系统的内存、硬盘、CPU等信息检查
- 正则表达式
- Android - Jar mismatch! Fix your dependencies问题解决