Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls
- 总时间限制:
- 1000ms
- 内存限制:
- 262144kB
- 描述
-
Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever.
Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts to duplicate itself right away. For every unit of time, each existing ball on the plane will duplicate itself, and the new balls will be put on the adjacent locations. The duplication rule of these balls is, during the i-th unit of time, a ball, which locates at (x, y), will duplicate ui balls to (x, y+1), di balls to (x, y-1), li balls to (x-1, y) and ri balls to (x+1, y).
The duplication rule has a period of M. In another words, ui=ui-M, di=di-M, li=li-M, ri=ri-M, for i=M+1,M+2,...
Wenwen is very happy because she will get many balls. It is easy to calculate how many balls she will get after N units of time. However, she wants to know the sum of x-coordinates and y-coordinates of all balls after N units of time. This is a bit difficult for her. Could you help her? Since the sum might be very large, you should give the sum modulo 1,000,000,007 to her.
- 输入
- The first line contains an integer T (1 ≤ T ≤ 25), indicating the number of test cases.
For each test case:
The first line contains four integers N (1 ≤ N ≤ 10^18), M (1 ≤ M ≤ 20,000), x0 and y0 (-10^18 ≤ x0,y0 ≤ 10^18);
Then follows M lines, the i-th line contains four integers: ui, di, li and ri (0 ≤ ui,di,li,ri ≤ 10,000).
- 输出
- For each test case, output one integer on a single line, indicating the sum of x-coordinates and y-coordinates of all balls after N units of time, modulo 1,000,000,007.
- 样例输入
-
1
2 2 1 1
2 0 0 0
0 0 0 1 - 样例输出
-
19
- 提示
- In the Sample Input:
Initially, there is 1 ball on (1,1).
After 1 unit of time, there is 1 ball on (1,1) and 2 balls on (1,2);
After 2 units of time, there is 1 ball on (1,1), 2 balls on (1,2), 1 ball on (2,1) and 2 balls on (2,2).
- Therefore, after 2 units of time, the sum of x-coordinates and y-coordinates of all balls is
(1+1)*1+(1+2)*2+(2+1)*1+(2+2)*2=19. - 题意:
- 给你一个球 初始位置在x0,y0
- 和一个周期函数
- 这个周期是m天 每天向上复制Ui个球 向下复制Di个球 向左复制Li个球,向右复制Ri个球
- 问你n天后 所有球的横纵坐标相加总和是多少
- 题解:
- Si表示第i天答案的总和, sumi 表示第i天球的总和
- S0为初始位置的答案即x+y
- 设定ai = ui+ri+li+di+1 , bi = ui+ri-li-di;
- 很容易得出
-
Si = S0 * (∏ai)+ ∑j ((∏ai)* bj / a[j]) ;
分别用逆元快速幂求解
-
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 2e5+, M = 1e2+, MOD = 1e9+, inf = 2e9;
typedef long long ll; ll update(ll x) {
return ((x % MOD)+MOD)%MOD;
} ll quick_pow(ll x,ll p) {
if(!p) return ;
ll ans = quick_pow(x,p>>);
ans = ans*ans%MOD;
if(p & ) ans = ans*x%MOD;
return ans;
} ll inv(ll x,ll mo)
{
return quick_pow(x,mo-);
} int T;
ll n;
ll m;
ll U[N],D[N],R[N],L[N],A[N],B[N],Y[N];
int main()
{
scanf("%d",&T);
while(T--) {
ll x,y;
scanf("%lld%lld%lld%lld",&n,&m,&x,&y);
ll S0 = (x+y )%MOD;
ll allA = ;
for(int i=;i<=m;i++) {
scanf("%lld%lld%lld%lld",&U[i],&D[i],&L[i],&R[i]);
A[i] = (U[i] + R[i] + L[i] + D[i] + ) % MOD;
B[i] = (U[i] + R[i] - L[i] - D[i]) % MOD;
allA = allA * A[i] % MOD;
} ll W = ;
for(int i=;i<=m;i++) W = (W + B[i] * inv(A[i],MOD) ) % MOD;
//cout<<W<<endl;
ll ans = S0 * update(quick_pow(allA, n / m)) % MOD + update(n/m) * W % MOD * quick_pow(allA, n/m)% MOD;
ans %= MOD;
ll sum = quick_pow(allA,n/m);
//cout<<ans<<" "<<sum<<endl;
for(int i=;i<=n%m;i++) {
ans = (ans * (A[i]) % MOD + sum * B[i] % MOD) % MOD;
sum = sum * (A[i]) % MOD;
}
printf("%lld\n",(ans+MOD )%MOD); }
return ;
}
Open judge C16H:Magical Balls 快速幂+逆元的更多相关文章
- HDU 5685 Problem A | 快速幂+逆元
Problem A Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu5698瞬间移动(杨辉三角+快速幂+逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元
题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...
- 51nod 1013【快速幂+逆元】
等比式子: Sn=(a1-an*q)/(1-q) n很大,搞一发快速幂,除法不适用于取膜,逆元一下(利用费马小定理) 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p).刚好 ...
- 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元
题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...
- 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学
题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...
- HDU 5793 A Boring Question (找规律 : 快速幂+逆元)
A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...
- 2016 Asia Jakarta Regional Contest J - Super Sum UVALive - 7720 【快速幂+逆元】
J-Super Sum 题目大意就是给定N个三元组<a,b,c>求Σ(a1^k1*a2^k2*...*ai^ki*..an^kn)(bi<=ki<=ci) 唉.其实题目本身不难 ...
- POJ 1845:Sumdiv 快速幂+逆元
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16466 Accepted: 4101 Descripti ...
随机推荐
- 如何让VS2013编写的程序
总体分c++程序和c#程序 1.c++程序 这个用C++编写的程序可以经过设置后在XP下运行,主要的“平台工具集”里修改就可以. 额外说明:(1)程序必须为Dotnet 4.0及以下版本.(XP只支持 ...
- bat批处理设置Java JDK系统环境变量文件
自己修改第3行的Java安装目录就可以设置JAVA_HOME, classPath,追加到PATH的最前面 JAVA_HOME=C:\Program Files\Java\jdk1.6.0_10 cl ...
- JS 基本语句
1.循环中必备的条件: 初始值 循环条件 状态改变 循环体 for(初始值 循环条件 状态改变) { 循环体 } for(var i=0;i<100;i++ ...
- Bootstrap弹窗插件(拟态框)关闭后回调函数
$(function(){ $('#questionnaire').on('hidden.bs.modal',function(){ //清除缓存方法 }); }); 事件 Bootstrap 的模态 ...
- Sql存储过程
下面的存储过程从四个表的联接中返回所有作者(提供了姓名).出版的书籍以及出版社.该存储过程不使用任何参数. USE pubs IF EXISTS (SELECT name FROM sysobject ...
- 闭包(block)
block主要解决反向传值和传值问题 1.block申明的公式 返回值类型 (^名字)(参数列表); 2.block实现的公式 名字= ^(参数列表){}; 3.局部变量 ...
- 使用a标签删除进行提示
一句话搞定: <a href="Login.aspx" target="mainFrame" class="STYLE4" oncli ...
- tableView设置首尾
[self.tableView setTableHeaderView:view]; [self.tableView setTableFooterView:view];
- 模拟赛1101d2
幸运数字(number)Time Limit:1000ms Memory Limit:64MB题目描述LYK 最近运气很差,例如在 NOIP 初赛中仅仅考了 90 分,刚刚卡进复赛,于是它决定使用一些 ...
- Mac与iPhone屏幕录制
1. Mac电脑屏幕录制 1.1 文件->新建屏幕录制 1.2 点击红色按钮 1.3 截取需要录制的屏幕部分,点击开始录制 1.4 点击工具栏的停止按钮,停止录制 1.5 然后会 ...