首页 > 其他 > 详细

NAIPC 2019-It’s a Mod, Mod, Mod, Mod World(类欧几里德模板)

时间:2019-09-05 19:50:06      阅读:279      评论:0      收藏:0      [点我收藏+]

It’s a Mod, Mod, Mod, Mod World

时间限制: 2 Sec  内存限制: 128 MB

题目描述

You are given multiple problems with three integers p, q, and n. Find 技术分享图片. That is, the first n multiples of p, modulo q, summed. Note that the overall sum has no modulus.

 

输入

Each input will begin with a line with a single integer W (1≤W≤105 ), which is the number of cases you must solve.
Each of the next W lines will contain three space-separated integers p, q and n (1≤p, q, n≤106 ),which are the parameters of the problem as described above.

 

输出

Output W lines, each with the answer for a given instance of the problem, in the order that they appear in the input.

样例输入

3
2 7 2
1 4 5
3 8 10

样例输出

6
7
37
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e5+7;
 5 const ll mod=1e9+7;
 6 ll p,q,n;
 7 //a 公差 b 首项 c 除数 n 项数
 8 ll work(ll a,ll b,ll c,ll n){
 9     if (n<=0) return 0;
10     if (n==1) return b/c ;
11     ll t = 0;
12     t += a/c*(n-1)*n/2+ b/c*n;
13     a = a%c;
14     b = b%c;
15     if (a==0) return t;
16     return t+work(c,(a*n+b)%c,a,(a*n+b)/c);
17 }
18 int main()
19 {
20     int t;
21     scanf("%d",&t);
22     while(t--)
23     {
24         scanf("%lld%lld%lld",&p,&q,&n);
25         ll ans=(p*(n+1)*n/2-q*work(p,p,q,n));
26         printf("%lld\n",ans);
27     }
28     return 0;
29 }

 



NAIPC 2019-It’s a Mod, Mod, Mod, Mod World(类欧几里德模板)

原文:https://www.cnblogs.com/CharlieWade/p/11469097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!