HDU 3221 Brute-force Algorithm


#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
int euler[maxn]; ll a,b,m,n; int phi()
{
for(int i=;i<maxn;i++) euler[i]=i;
for(int i=;i<maxn;i++)
{
if(euler[i]==i)
{
for(int j=i;j<maxn;j+=i)
{
euler[j]=euler[j]/i*(i-);
}
}
}
} struct Matrix
{
ll a[][];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
res.a[i][j]+=a[i][k]*p.a[k][j];
if(res.a[i][j]>euler[m])//特别要注意这个条件
{
res.a[i][j]=res.a[i][j]%euler[m]+euler[m];
}
}
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,ll n)
{
Matrix res;
for(int i=;i<;i++)
{
res.a[i][i]=;
}
while(n)
{
if(n&) res=res*base;
base=base*base;
n>>=;
}
return res;
} ll pow(ll a,ll n)
{
ll ans=;
while(n)
{
if(n&) ans=ans*a%m;
a=a*a%m;
n>>=;
}
return ans;
} void Matrix_init()
{
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
} int main()
{
int t,cas=;
ll ansa,ansb,faca,facb;
phi();
scanf("%d",&t);
while(t--)
{
cas++;
scanf("%lld%lld%lld%lld",&a,&b,&m,&n);
printf("Case #%d: ",cas);
if(n==) printf("%lld\n",a%m);
else if(n==) printf("%lld\n",b%m);
else if(n==) printf("%lld\n",a*b%m);
else if(m==) printf("0\n");
else
{
Matrix_init();
ans=ans*quick_pow(base,n-);
faca=ans.a[][];
facb=ans.a[][];
ansa=pow(a,faca)%m;
ansb=pow(b,facb)%m;
printf("%lld\n",ansa*ansb%m);
}
}
return ;
}
HDU 3221 Brute-force Algorithm的更多相关文章
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 6215 Brute Force Sorting(链表)
http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...
- hdu 6215 -- Brute Force Sorting(双向链表+队列)
题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...
- hdu 6215 Brute Force Sorting(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...
- HDU 6215 Brute Force Sorting 模拟双端链表
一层一层删 链表模拟 最开始写的是一个一个删的 WA #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) mem ...
- HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting
Brute Force Sorting Time Limit: 1 Sec Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 4971 A simple brute force problem.
A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged o ...
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意 ...
- Test SRM Level Three: LargestCircle, Brute Force
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...
- hdu6215 Brute Force Sorting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...
随机推荐
- svn 服务器搭建
http://www.cnblogs.com/wrmfw/archive/2011/09/08/2170465.html 一,安装必须的软件包. $ apt-get install subversio ...
- HTML第二本书学习后记
1,换行标记<br>,不换行标记<nobr></nobr> 2,添加水平线 <hr> 3,插入特殊符号: 空格   " ...
- 理解AX InventTrans的几种状态
接触AX一段时间后,发现InventTrans表非常重要.它可以说是物流模块的核心,开发BI报表的话必须依赖此报表. 先来看官方释义: InventTrans表包含了库存交易的信息.当销售/采购订 ...
- mac eclipse 添加pydev插件 步骤
前提:eclipse环境可以正常使用 python环境可以正常使用 1.下载pydev(http://www.pydev.org/)
- CMake的一些使用
1. 使用QT加载CMake工程 打开QT,"文件"->"打开文件或项目"->选中CMakeLists.txt文件,出现对话框,单击下一步,点击&q ...
- 如何修改MyEclipse项目的web context-root
修改一个MyEclipse项目的名称很容易,右键点项目->rename就行了. 但此时项目的web context-root 还没有改变,需要右键点项目->properties->M ...
- (转载)spring mvc DispatcherServlet详解之一---处理请求深入解析
要深入理解spring mvc的工作流程,就需要先了解spring mvc的架构: 从上图可以看到 前端控制器DispatcherServlet在其中起着主导作用,理解了DispatcherServl ...
- 使用HTTPS网站搭建iOS应用内测网站(OTA分发iOS应用)
为什么要搭建应用内测网站呢? 1.AppStore的审核速度比较慢,万一被拒,还得等,而且一旦发布,任何人都可以下载,而有些时候只有老板想知道最新的修改是否符合要求,万一不符合要求呢?又要修改了. 2 ...
- haproxy para config
.. from http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html 常用配置选项: OPTION 选项: option http ...
- python file operations
原文地址 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当 ...