链接:  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

  1. Input:
  2.  
  3. 3
  4. 4 2 1
  5. 4 2 2
  6. 4 2 3
  7.  
  8. Output:
  9.  
  10. 2
  11. 1000000005
  12. 1000000003
  13.  
  14. 贴个代码
  15.  
  16. 吃个教训,在february challenge 期间就传了代码,然后有人直接就这个代码交了,被codechef发邮件通知掉rating了,以后不敢了
  1. #include <bits/stdc++.h>
  2. #define mst(a,b) memset((a),(b), sizeof a)
  3. #define lowbit(a) ((a)&(-a))
  4. #define IOS ios::sync_with_stdio(0);cin.tie(0);
  5. using namespace std;
  6. typedef long long ll;
  7. const int mod=1e9+;
  8. const int maxn=1e6+;
  9. const ll INF = 1LL<<;
  10. const int N=;
  11. ll qpow(ll a,ll b){
  12. ll ret=;
  13. while(b){
  14. if(b&)ret=ret*a%mod;
  15. b>>=;a=a*a%mod;
  16. }
  17. return ret;
  18. }
  19. struct matrix{
  20. ll mat[N][N];
  21. matrix operator*(const matrix&m)const{
  22. matrix tmp;
  23. for(int i=;i<N;++i){
  24. for(int j=;j<N;++j){
  25. tmp.mat[i][j]=;
  26. for(int k=;k<N;++k){
  27. tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%mod;
  28. tmp.mat[i][j]%=mod;
  29. }
  30. }
  31. }
  32. return tmp;
  33. }
  34. };
  35. void solve(ll d,ll l,ll t,ll&a,ll&b){
  36. ll gg=__gcd(d,l);d/=gg,l/=gg;l%=mod,d%=mod;
  37.  
  38. b=qpow(l,t);--t;
  39. matrix m,ans;
  40. mst(m.mat,);mst(ans.mat,);
  41. for(int i=;i<N;++i)ans.mat[i][i]=;
  42. m.mat[][]=*d%mod;m.mat[][]=-(l*l%mod);
  43. m.mat[][]=;
  44. while(t){
  45. if(t&)ans=ans*m;
  46. t>>=;
  47. m=m*m;
  48. }
  49. a=(ans.mat[][]*d%mod+ans.mat[][]%mod)%mod;
  50. a=(a+mod)%mod;
  51. }
  52. int main(){
  53. #ifdef local
  54. freopen("in.txt","r",stdin);
  55. //freopen("out.txt","w",stdout);
  56. #endif
  57. int t;scanf("%d",&t);
  58. while(t--){
  59. ll d,l,t;scanf("%lld%lld%lld",&l,&d,&t);
  60. ll a,b;solve(d,l,t,a,b);
  61. // cout<<a<<" "<<b<<endl;
  62. printf("%lld\n",l*a%mod*qpow(b,mod-)%mod);
  63. }
  64. return ;
  65. }

搞了好久,一直在想怎么用分数表示一个实数,是自己太傻逼了

用极坐标 (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(极坐标+三角函数递推+矩阵快速幂)的更多相关文章

  1. CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)

    题目链接  Broken Clock   中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 我们现在 ...

  2. 2018.10.09 NOIP模拟 路途(递推+矩阵快速幂优化)

    传送门 签到题.(考试的时候写挂爆0) 令AiA_iAi​表示邻接矩阵的iii次幂. 于是就是求Al+Al+1+...+ArA_l+A_{l+1}+...+A_rAl​+Al+1​+...+Ar​. ...

  3. codechef February Challenge 2018 简要题解

    比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...

  4. CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)

    题目链接  Points Inside A Polygon 题意  给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...

  5. 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 ...

  6. 2018.10.19 NOIP模拟 硬币(矩阵快速幂优化dp)

    传送门 不得不说神仙出题人DZYODZYODZYO出的题是真的妙. f[i][j][k]f[i][j][k]f[i][j][k]表示选的硬币最大面值为iii最小面值不小于jjj,总面值为kkk时的选法 ...

  7. 2018.08.30 NOIP模拟 kfib(矩阵快速幂+exgcd)

    [输入] 一行两个整数 n P [输出] 从小到大输出可能的 k,若不存在,输出 None [样例输入 1] 5 5 [样例输出] 2 [样例解释] f[0] = 2 f[1] = 2 f[2] = ...

  8. 【2018北京集训十二】 coin 矩阵快速幂

    矩阵快速幂原来还可以这么用?? 你们城里人还真会玩. 我们令$f[i][j][k]$表示总的钱数为i,当前使用的最大面值硬币的面值为$v_j$,最小为$v_k$的方案数量. 不难发现$f[i][j][ ...

  9. Codechef October Challenge 2018 游记

    Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...

随机推荐

  1. 设计模式之单例模式(Singleton Pattern)

    单例模式是最简单的设计模式之一.属于创建型模式,它提供了一种创建对象的最佳方式.使应用中只存在一个对象的实例,并且使这个单实例负责所有对该对象的调用.这种模式涉及到一个单一的类,该类负责创建自己的对象 ...

  2. sys模块&json模块&pickle模块

    sys模块&json模块&pickle模块 sys模块 一.导入方式 import sys 二.作用 与Python解释器交互 三.模块功能 3.1 经常使用 sys.path #返回 ...

  3. Django之AJAX传输JSON数据

    目录 Django之AJAX传输JSON数据 AJAX 中 JSON 数据传输: django响应JSON类型数据: django 响应 JSON 类型数据: Django之AJAX传输JSON数据 ...

  4. oracle 常用查询语句

    一.一般日常用的脚本 1.检查源库每个节点至少3组redoselect group#,thread#,bytes/1024/1024,members,status from v$log; select ...

  5. java学习笔记(5)多线程

    一.简介(过段时间再写,多线程难度有点大) --------------------------------------- 1.进程:运行时的概念,运行的应用程序 2.线程:应用程序内部并发执行的代码 ...

  6. vue-cli常用插件安装教程

    1.安装sass npm i sass-loader node-sass --save-dev 2.安装stylus cnpm install stylus --save-dev cnpm insta ...

  7. git 查看对比的方法log diff

    git shortlog 默认情况下,git shortlog 把输出按作者名字排序,但你可以传入 -n 选项来按每个作者提交数量排序. 1.有冲突时可以用 git status查看 2.通过git ...

  8. 关于jQuery获取不到动态添加的元素节点的问题

    遇到问题: 当我获取 $("#art-list")页面元素后去在后面追加标签的时候(append),在下面用 $(selector) 获取刚刚添加的标签,发现怎么都获取不到. 问题 ...

  9. ShareSdk等等(三方登录与支付冲突问题)

    1.必须实现前两个方法,第三个方法照成支付回调有问题. //必须实现的方法 - (BOOL)application:(UIApplication *)application handleOpenURL ...

  10. apache的rewrite机制

    当我们使用thinkphp的时候,比如说我们访问一个Test控制器的test方法,http://localhost/index.php/Test/test/1.html,那个这个1是用get方式传递的 ...