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 ...
随机推荐
- poj 1001
http://poj.org/problem?id=1001 这是一道高精度的运算,如果你之前有写过那种高精度的乘法的题目的话,做这个也还是比较简单的.. 思路:我是首先把小数点的位置确定下来,然后其 ...
- selenium 右键下载图片,结合sikuli
上一次写右键下载是结合robot,这次是使用selenium+sikuli 上一次日志:http://www.cnblogs.com/tobecrazy/p/3969390.html 有关sikuli ...
- C++ STL 的实现:
C++ STL 的实现: 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque 底层数据结构为一个中央控制器和多个缓 ...
- Effective C++ -----条款38:通过复合塑模出has-a或“根据某物实现出”
复合(composition)的意义和public继承完全不同. 在应用域(application domain),复合意味has-a(有一个).在实现域(implementation domain) ...
- Effective C++ -----条款33:避免遮掩继承而来的名称
derived classes内的名称会遮掩base classes内的名称.在public继承下从来没有人希望如此. 为了让被遮掩的名称再见天日,可使用using声明式或转交函数(forwardin ...
- 字符串与byte数组转换
string weclome=""; byte[] data = new byte[1024]; //字符串转byte数组 data = Encoding.ASCII.GetByt ...
- LightOJ 1197 Help Hanzo(区间素数筛选)
E - Help Hanzo Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- java导出excel(解决导出几万条数据内存溢出的问题)
import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import ja ...
- Hibernate双向一对多对象关系模型映射
双向one-to-many 描述部门和岗位:一个部门有多个岗位 将单向的one-to-many 和many-to-one合并. 4.1双向的one-to-many数据库模型 create table ...
- [Android Pro] Java进阶学习:jar打包详解
jar文件听说过吗,没有?或者陌生!好,没关系,这就是我们的第一站:打包发布. 为什么会有这个玩意呢,首先,这是jar的全称:JavaTM Archive (JAR) file,是的,就是java存档 ...