Geometric Sum

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述
Compute (a + a^2 + … + a^n) mod m.(a+a2+…an)mod
 
输入
Three integers a,n,m.
(1≤a,n,m≤10^18)
It ends with EOF.
输出
The only integer denotes the result.
样例输入
2 2 1000000000
样例输出
6 
来源
Lepus
 矩阵里也求过a+a^2+a^3+a^4.......
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long LL; LL sum_mod(LL a,LL n,LL p)
{
LL ans=;
n=n%p;
while(n)
{
if(n&)
{
ans=ans+a;
if(ans>=p) ans=ans-p;
}
n=n>>;
a=(a+a)%p;
}
return ans;
}
LL solve(LL a,LL n,LL p)
{
LL p1=a,p2=a,ans,i;
vector<LL>Q;
while(n)
{
ans=(n&);
Q.push_back(ans);
n=n>>;
}
ans=Q.size()-;
for(i=ans;i>=;i--)
{
p1=sum_mod(p1,p2+,p);
p2=sum_mod(p2,p2,p);
if(Q[i]==)
{
p2=sum_mod(p2,a,p);
p1=(p1+p2)%p;
}
}
return p1;
}
int main()
{
LL a,n,p;
while(scanf("%lld%lld%lld",&a,&n,&p)>)
{
if(n==){
printf("%lld\n",a%p);
continue;
}
LL ans=solve(a,n,p);
printf("%lld\n",ans);
}
return ;
}

nylg 640 Geometric Sum的更多相关文章

  1. 基本概率分布Basic Concept of Probability Distributions 3: Geometric Distribution

    PDF version PMF Suppose that independent trials, each having a probability $p$, $0 < p < 1$, o ...

  2. geometric median

    The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing ...

  3. Codeforces Round #Pi (Div. 2) C. Geometric Progression map

    C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. 补写:Best Coder #85 1001 Sum(前缀和)

    sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...

  5. sum (bestcoder)

    sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...

  6. Codeforces Round #Pi (Div. 2) C. Geometric Progression

    C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. codeforces 789 B. Masha and geometric

    链接 B. Masha and geometric depression 题意 给你一个等比数列的首项和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的可以写 ...

  8. Java实现 LeetCode 640 求解方程(计算器的加减法计算)

    640. 求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回" ...

  9. 图神经网络 PyTorch Geometric 入门教程

    简介 Graph Neural Networks 简称 GNN,称为图神经网络,是深度学习中近年来一个比较受关注的领域.近年来 GNN 在学术界受到的关注越来越多,与之相关的论文数量呈上升趋势,GNN ...

随机推荐

  1. poj 2509 Peter's smokes

    http://poj.org/problem?id=2509 Peter's smokes Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. csuoj 1396: Erase Securely

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1396 1396: Erase Securely Time Limit: 1 Sec  Memory ...

  3. Java基础(41):Java中集合中需要注意的几个要点(关于Collection与Map)

    同步性     Vector是同步的.这个类中的一些方法保证了Vector中的对象是线程安全的.而ArrayList则是异步的,因此ArrayList中的对象并 不是线程安全的.因为同步的要求会影响执 ...

  4. linux第10天 msg消息队列

    cat /proc/sys/kernel/msgmax最大消息长度限制 cat /proc/sys/kernel/msgmnb消息队列总的字节数 cat /proc/sys/kernel/msgmni ...

  5. URAL 1001 Reverse Root(水题?)

    The problem is so easy, that the authors were lazy to write a statement for it! Input The input stre ...

  6. Android -- FragmentTabHost实现微信底部切换

    1,在商城类的项目中我们开始一个项目的时候经常出现这样的需求,如下图所示: 下面使用户可以切换的模块,上面是对应的模块的详细内容,实现这个效果有很多方式,可以使用radiobutton+fragmen ...

  7. fackbook的Fresco的多种图片加载方法以及解码过程

    上篇文章中我们提到了图片加载其实是用了三条线程,如果没看过的同学可以先了解下这里. fackbook的Fresco的Image Pipeline以及自身的缓存机制 那么今天我们就来探索一下如何在代码中 ...

  8. 链接库lib和dl的概念,加载方式的区别

    使用LR进行基于windows socket协议做接口测试,只提供了lr_load_dll方法来动态加载动态链接库.之前学习阶段,对TinyXML的学习,使用的静态链接库,当时在程序调用的时候方法也跟 ...

  9. [tp3.2.1]开启URL(重写模式),省略URL中的index.php

    重写模式(省略url中的index.php) 在apache配置文件httpd.conf中,查找 1.mod_rewrite.so, 启动此模块 2.AllowOverride , 值= All 3. ...

  10. Sql Server 2012 的新分页方法分析(offset and fetch) - 转载

    最近在分析 Sql Server 2012 中 offset and fetch 的新特性,发现 offset and fetch 无论语法的简洁还是功能的强大,都是相当相当不错的 其中 offset ...