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

一时失言乱红尘 2023-06-05 12:33 16阅读 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 20190401214301_71285.png. 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.

样例输入

  1. 3
  2. 2 7 2
  3. 1 4 5
  4. 3 8 10

样例输出

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

转载于:https://www.cnblogs.com/CharlieWade/p/11469097.html

发表评论

表情:
评论列表 (有 0 条评论,16人围观)

还没有评论,来说两句吧...

相关阅读

    相关 GO mod入门

    借鉴帖子,特别感谢: 作者:会飞的鲶鱼 链接:https://www.jianshu.com/p/c666ebdb462b   Go Module是Go官方在1.11

    相关 oracle mod函数

    mod(m,n) ![这里写图片描述][SouthEast] (1)MOD返回m除以n的余数,如果n是0,返回m。 (2)这个函数以任何数字数据类型或任何非数值型数

    相关 Mod (二分法)

    Kim刚刚学会C语言中的取模运算(mod)。他想要研究一下一个数字A模上一系列数后的结果是多少。帮他写个程序验证一下。 Input 第一行一个整数T代表数据组数。 接下来