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 ...
随机推荐
- CentOS 6.5 安装Python 3.5
1.CentOS6.5 安装Python 的依赖包 yum groupinstall "Development tools" yum install zlib-devel bzip ...
- poj4052
题意:求一个文章(长度5.1e6)里面出现了多少个指定的模式串.重复出现只记一次.而且如果两个模式串都出现的情况下,一个是另一个的子串,则该子串不算出现过. 分析:AC自动机. 由于子串不算所以加一些 ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- Python 之 【markdown 模块的学习】
摘要: markdown工具,可以将txt转化成html格式.这一类工具的作用是将按一定格式写成的可读性强的文本文件转化为结构化的标准xhtml或html.Linux 下面也有markdown: zh ...
- 实现Windows Phone 8中ListBox的分页加载
功能就是ListBox滚动到最下方的时候,能够自动加载下一页的内容. 解决问题的关键就是如何判断ListBox已经加载到了最底部. 网上找了两个解决方法: 1 http://googlers.itey ...
- tp5中的一些小方法
// 当使用一个新页面替换当前页面的body后,body刷新了,所选择的select值就不能保存住,解决方法如下: 作业题目<select> <option>--请选择--&l ...
- IOS - 消息推送原理和实现
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...
- [Android Pro] 使用apktool工具遇到could not decode arsc file的解决办法
转:http://www.cnblogs.com/sage-blog/p/4323049.html 最近使用APKtool工具反编译APK老是提示不成功,错误如下: Exception in thre ...
- JS_ECMA基本语法中的几种封装的小函数-1
今天给大家介绍js ECMA中几个封装的小函数以及一些常用的函数小案例: 1,找重复的函数 <script> //在数组里面找重复: function findInArr(n,arr){ ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(十) 之 自定义系统消息和总结
前言 本篇主要讲解一个东西,就是我们自定义系统消息.效果如下: 首先我们要做的准备工作就是改写 layim 的消息模板,如果不改的话就成为某个用户发送的消息了,那么体验就稍微差一些.找到模板我们看一下 ...