HNU 11720 God Created The Integers

  原题传送:http://acm.hnu.cn/online/?action=problem&type=show&id=11720&courseid=0

  对于这条式子:

  

  和下面的式子是等价的:

  Sp = (p2 - 1) / 2 - (p - 1) / 4

  那么求出Sp后有rp*Sp ≡ 1 (mod p),用扩展GCD求出rp就行了。

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 typedef __int64 LL;
 5 LL p, s;
 6 
 7 LL exgcd(LL a, LL b, LL &x, LL &y)
 8 {
 9     LL d, t;
10     if(b == 0)
11     {
12         x = 1, y = 0;
13         return a;
14     }
15     d = exgcd(b, a % b, x, y);
16     t = x, x = y, y = t - (a / b) * x;
17     return d;
18 }
19 
20 void solve(int cas)
21 {
22     s =(p * p - 1) / 24 - (p - 1) / 4;
23     LL x, y;
24     exgcd(s, p, x, y);
25     printf("Case #%d: %I64d\n", cas, (x + p) % p);
26 }
27 
28 int main()
29 {
30     int t, cas;
31     while(scanf("%d", &t) != EOF)
32     {
33         for(cas = 1; cas <= t; cas ++)
34         {
35             scanf("%I64d", &p);
36             solve(cas);
37         }
38     }
39     return 0;
40 }

 

  

转载于:https://www.cnblogs.com/huangfeihome/archive/2012/10/06/2713070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值