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

最新文章

  1. 转战网站后台与python
  2. WP小游戏产品海外发行经验小结
  3. div中实现居中
  4. ios NSString 去除空格和回车
  5. Juicy Couture_百度百科
  6. Linux下的在线播放神器
  7. Eclipse代码自动提示设置
  8. syntaxhighlighter的使用
  9. java.lang.IllegalArgumentException: Document base E:\Eclipse\workspace\.metadata\.plugins\org.eclips
  10. python/零起点(一、字符串)
  11. Python基础之Windows下Python3.x环境搭建
  12. linux小计
  13. PSP耗时
  14. C#:TextBox数据绑定
  15. js中json对象数组按对象属性排序---1
  16. js 模拟call、apply、bind实现
  17. L256 翻译
  18. 使用uni-app开发微信小程序之登录模块
  19. 使用MVVM设计模式构建WPF应用程序
  20. 【转】Castle.ActiveRecord的嵌套事务处理

热门文章

  1. 面试mysql表设计要注意啥
  2. SparkML之推荐算法ALS
  3. 深入理解java虚拟机(1)走进jvm
  4. JVM内存结构思维导图
  5. 修改jar包package目录结构操作方法
  6. 一、JS基本基础
  7. 关于Unsupported major.minor version 52.0报错问题解决方案
  8. iOS常用数学常量宏
  9. Linux系统账户管理指令
  10. (转)JAVA socket 进行十六进制报文交互测试