CodeChef-----February Challenge 2018---Broken Clock(极坐标+三角函数递推+矩阵快速幂)
链接: https://www.codechef.com/FEB18/problems/BROCLK
Broken Clock Problem Code: BROCLK
Chef has a clock, but it got broken today — the minute hand on Chef's clock doesn't rotate by the angle 2π/3600 each second, but by a different fixed angle x. The coordinates of the center of the clock are (0, 0). The length of the minute hand is l.
One endpoint of the minute hand is always located at the clock center; the other endpoint is initially located at the point (0, l). One second later, Chef observes that this endpoint is at distance d above the x-axis, i.e. the y-coordinate of this endpoint is equal to d.
Chef is curious about where the minute hand will be (specifically, its y-coordinate) after tseconds. Because t can be very large, Chef can't wait for that moment. Please help him!
Input
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains three space-separated integers l, dand t.
Output
We can prove that for the given constraints, the y-coordinate of the end of the minute hand can always be written as a rational number p / q, where gcd(p, q) = gcd(q, 109 + 7) = 1. Let's denote the modular inverse of q (it's guaranteed that the modular inverse exists and is unique) by r.
For each test case, print a single line containing one number (p · r) modulo 109 + 7.
Constraints
- 1 ≤ T ≤ 105
- 1 ≤ d < l ≤ 109
- 1 ≤ t ≤ 1018
Subtasks
Subtask #1 (5 points): t ≤ 3
Subtask #2 (15 points): t is a power of 2, i.e. t = 2p for some p ≥ 0
Subtask #3 (40 points): sum of t over all test cases ≤ 106
Subtask #4 (40 points): original constraints
Example
Input: 3
4 2 1
4 2 2
4 2 3 Output: 2
1000000005
1000000003 贴个代码 吃个教训,在february challenge 期间就传了代码,然后有人直接就这个代码交了,被codechef发邮件通知掉rating了,以后不敢了
#include <bits/stdc++.h>
#define mst(a,b) memset((a),(b), sizeof a)
#define lowbit(a) ((a)&(-a))
#define IOS ios::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;
const int mod=1e9+;
const int maxn=1e6+;
const ll INF = 1LL<<;
const int N=;
ll qpow(ll a,ll b){
ll ret=;
while(b){
if(b&)ret=ret*a%mod;
b>>=;a=a*a%mod;
}
return ret;
}
struct matrix{
ll mat[N][N];
matrix operator*(const matrix&m)const{
matrix tmp;
for(int i=;i<N;++i){
for(int j=;j<N;++j){
tmp.mat[i][j]=;
for(int k=;k<N;++k){
tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%mod;
tmp.mat[i][j]%=mod;
}
}
}
return tmp;
}
};
void solve(ll d,ll l,ll t,ll&a,ll&b){
ll gg=__gcd(d,l);d/=gg,l/=gg;l%=mod,d%=mod; b=qpow(l,t);--t;
matrix m,ans;
mst(m.mat,);mst(ans.mat,);
for(int i=;i<N;++i)ans.mat[i][i]=;
m.mat[][]=*d%mod;m.mat[][]=-(l*l%mod);
m.mat[][]=;
while(t){
if(t&)ans=ans*m;
t>>=;
m=m*m;
}
a=(ans.mat[][]*d%mod+ans.mat[][]%mod)%mod;
a=(a+mod)%mod;
}
int main(){
#ifdef local
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
int t;scanf("%d",&t);
while(t--){
ll d,l,t;scanf("%lld%lld%lld",&l,&d,&t);
ll a,b;solve(d,l,t,a,b);
// cout<<a<<" "<<b<<endl;
printf("%lld\n",l*a%mod*qpow(b,mod-)%mod);
}
return ;
}
搞了好久,一直在想怎么用分数表示一个实数,是自己太傻逼了
用极坐标 (l,ρ)表示点,那么y就等于l*cosρ,题目给你的一秒转角α的cos值是d/l,因此只要求出t秒转过角度的cos值答案就出来啦
所以怎么求cos(tα)嘞?
由于cos(A+B)=cosAcosB-sinAsinB
cos(A-B)=cosAcosB+sinAsinB
两式相加得cos(A+B)+cos(A-B)=2cosAcosB
所以得到递推式 cos((t+1)α) = 2*cos(tα)cosα - cos((t-1)α)
用矩阵快速幂求出tα的cos值
怎么保证这个分数分子分母是互质的呢,因为递推式里是有分数相减的
先把d和l先除一个gcd(d,l)这样就可以先保证 d,l互质
然后因为分母一直是l的k次方,分子会是d的倍数减去l²的倍数
因为分子分母一开始互质,反证法可以证明分子分母在递推后依旧互质,所以可以大胆取模
得到cos(tα)的分数形式 a/b后,ans就是 l*a*inv(b)=l*a*pow(b,mod-2)
CodeChef-----February Challenge 2018---Broken Clock(极坐标+三角函数递推+矩阵快速幂)的更多相关文章
- CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)
题目链接 Broken Clock 中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 我们现在 ...
- 2018.10.09 NOIP模拟 路途(递推+矩阵快速幂优化)
传送门 签到题.(考试的时候写挂爆0) 令AiA_iAi表示邻接矩阵的iii次幂. 于是就是求Al+Al+1+...+ArA_l+A_{l+1}+...+A_rAl+Al+1+...+Ar. ...
- codechef February Challenge 2018 简要题解
比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...
- CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)
题目链接 Points Inside A Polygon 题意 给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...
- 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)
传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...
- 2018.10.19 NOIP模拟 硬币(矩阵快速幂优化dp)
传送门 不得不说神仙出题人DZYODZYODZYO出的题是真的妙. f[i][j][k]f[i][j][k]f[i][j][k]表示选的硬币最大面值为iii最小面值不小于jjj,总面值为kkk时的选法 ...
- 2018.08.30 NOIP模拟 kfib(矩阵快速幂+exgcd)
[输入] 一行两个整数 n P [输出] 从小到大输出可能的 k,若不存在,输出 None [样例输入 1] 5 5 [样例输出] 2 [样例解释] f[0] = 2 f[1] = 2 f[2] = ...
- 【2018北京集训十二】 coin 矩阵快速幂
矩阵快速幂原来还可以这么用?? 你们城里人还真会玩. 我们令$f[i][j][k]$表示总的钱数为i,当前使用的最大面值硬币的面值为$v_j$,最小为$v_k$的方案数量. 不难发现$f[i][j][ ...
- Codechef October Challenge 2018 游记
Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...
随机推荐
- scrapy之360图片爬取
#今日目标 **scrapy之360图片爬取** 今天要爬取的是360美女图片,首先分析页面得知网页是动态加载,故需要先找到网页链接规律, 然后调用ImagesPipeline类实现图片爬取 *代码实 ...
- Python基础数据类型str字符串
3.3字符串str ' ' 0 切片选取 [x:y] 左闭右开区间 [x:y:z] 选取x到y之间 每隔z选取一次(选取x,x+z,....) z为正 索引位置:x在y的左边 z为负 索引位置:x在y ...
- 【统计】Causal Inference
[统计]Causal Inference 原文传送门 http://www.stat.cmu.edu/~larry/=sml/Causation.pdf 过程 一.Prediction 和 causa ...
- Java Calendar使用
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- 手把手教你vue配置请求本地json数据
本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...
- $APIO~2019$ 游记
我是鸽子. Upd:我全国倒数第一稳了. Uupd:时间过去好久了,这段时间发生很多事,比如NOIP没了... APIO时候的事也记得不是很清楚了,随便写点颓废资料吧: 如果想吃离酒店最近的一家火锅店 ...
- LeetCode 338. 比特位计数
338. 比特位计数 题目描述 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 示例 1: 输入: 2 输出 ...
- npm学习(十)之如何使用创建、发布、使用作用域包
前言 要求npm版本2或更高 作用域用于将相关包分组在一起,并为npm模块创建一个名称空间(类似于域).这里有更详细的解释. 如果一个包的名称以@开头,那么它就是一个有作用域的包.范围是@和斜杠之间的 ...
- FP Style 的快排
const quickSort = (list) => { if (!list || !list.length) return []; if (list.length === 1) return ...
- Flask开发系列之模板
Flask开发系列之模板 本文对<FlaskWeb开发:基于python的Web应用开发实战>模板一节做的总结. Jinja2模板引擎 模板 模板是一个包含响应文本的文件,其中包含用占位变 ...