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 ...
随机推荐
- 使用bootstrap 弹出效果演示
前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...
- Delphi以及三方控件的源代码规模
这些项目大多数使用C++或者C编写,使用SourceCounter-3.5.33.73工具来统计源代码数量,本来是这里下载的: https://code.google.com/p/boomworks/ ...
- C# 日期格式转【转】
使用:DateTime.ToString的方法(String, IFormatProvider)转换格式 using System; using System.Globalization; Strin ...
- bash里面的一些符号说明
$$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 $* 所有参 ...
- async 和 await小结
三大返回值: 返回类型 - Task<TResult> 返回类型 - Task 返回类型 - void 当你添加 async 关键字后,需要返回一个将用于后续操作的对象,请使用 Task& ...
- Python学习之路【目录】
本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! 目录: Python学习[第一篇]python简介 Python学习[第二篇]p ...
- PHP访问带密码的Redis
1. 设置Redis密码,以提供远程登陆打开redis.conf配置文件,找到requirepass,然后修改如下: requirepass yourpassword yourpassword就是re ...
- telnet输入乱码的解决
1.Win+R --- 运行窗口 输入cmd回车 2.输入telnet 主机 端口 3.连接主机发现无法输入 4.这里什么也不要输入,按下 ctrl+] 键 5.按下回车键,然后会弹出新的窗口,就可 ...
- vim - Removing duplicate lines
http://vim.wikia.com/wiki/Uniq_-_Removing_duplicate_lines :sort u
- C#写入日志信息到文件中
为了在服务器上运行程序及时的跟踪出错的地方,可以在必要的地方加入写日志的程序. string folder = string.Format(@"D:\\{0}\\{1}", Dat ...