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. codeforces-Glass Carving(527C)std::set用法

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. poj: 3094

    简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...

  3. logstash学习2

    从geoip字段取location的longitude字段 [geoip][location][0] 支持变量内插 "the longitude is %{[geoip][location] ...

  4. 8007003Windows Update遇到未知错误

    如果在检查更新时收到 Windows Update 错误 80070003,则需要删除 Windows 用于标识计算机更新的临时文件.若要删除临时文件,请停止 Windows Update 服务,删除 ...

  5. C++中关于无法解析的外部符号问题LNK2019问题的总结

           网上一般有很全面的解决方法,最近恰好本道长也遇到了这种问题,也恰好解决了,这种问题应该算作配置问题,而非程序本身问题,多数是因为接手了生疏的程序导致,此问题看上去很简单,但木有经验的话很 ...

  6. 【HDNOIP】HD201404最短路径

    HD201404最短路径 [试题描述] a.b.c是3个互不相等的1位正数,用它们和数字0可以填满一个n行n列的方格阵列,每格中都有4种数码中的一个.填入0的格子表示障碍物,不能属于任何路径.你是否能 ...

  7. 160921、React入门教程第一课--从零开始构建项目

    工欲善其事必先利其器,现在的node环境下,有太多好用的工具能够帮助我们更好的开发和维护管理项目. 我本人不建议什么功能都自己写,我比较喜欢代码复用.只要能找到npm包来实现的功能,坚决不自己敲代码. ...

  8. access数据库导入Oracle

    1.对着当前的表右击->导出->选择下面的保存类型为"ODBC数据库"找一个路径输入文件名2.将x导出到x,点击->确定3.在弹出的对话框中DSN名称,点击-&g ...

  9. halcon学习笔记——机器视觉工程应用的开发思路【转】

    转自:http://www.cnblogs.com/hanzhaoxin/archive/2013/02/15/2912879.html 机器视觉工程应用主要可划分为硬件和软件两大部分. 硬件:工程应 ...

  10. 严重: IOException while loading persisted sessions: java.io.EOFException

    tomcat在启动时出现如下异常问题: 严重: IOException while loading persisted sessions: java.io.EOFException 严重: Excep ...