??xml version="1.0" encoding="utf-8" standalone="yes"?>久久免费视频1,亚洲伊人久久综合影院,亚洲狠狠综合久久http://m.shnenglu.com/sdfond/category/9806.html世事z明皆学问,人情l达x(chng)?/description>zh-cnSun, 05 Jul 2015 19:14:55 GMTSun, 05 Jul 2015 19:14:55 GMT60又翻Z道老题http://m.shnenglu.com/sdfond/archive/2010/04/26/113659.htmlsdfondsdfondMon, 26 Apr 2010 15:34:00 GMThttp://m.shnenglu.com/sdfond/archive/2010/04/26/113659.htmlhttp://m.shnenglu.com/sdfond/comments/113659.htmlhttp://m.shnenglu.com/sdfond/archive/2010/04/26/113659.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/113659.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/113659.html  l定一个a * b的网|从左下到右上M条线I过的格子数是n。给定一个n问有多少U不同的a和b满I过的格子数为n。对应TJU 2880?br>  首先n = a + b - gcd(a, b)。因为对于每个穿q的格子Q直U可能会(x)从格子的上侧I过Q也可能从格子的右侧I过Q也可能既穿q上侧也I过右侧(也就是从右上角穿q?。因为直U总要从左边走到右边,因此恰好有a个格子会(x)被直U穿q右?直线是连l的Q不?x)在同一列同时穿q两个格子的右侧)Q同理恰好有b个格子会(x)被直U穿q上侧,q样d是a + b个。但是这L(fng)话穿q右上角的格子就被重复计了(jin)Q这L(fng)格子如果坐标?x, y)Q一定满个条Ӟ(x)x : y = a : bQ这样ay = bxQ显然满等式的解个数是gcd(a, b)个。这样n的值就被计出来了(jin)?br>  然后设g = gcd(a, b)Qa = a' * g, b = b' * g, 那么n = (a' + b' - 1) * g, 其中gcd(a', b') = 1。通过枚Dg可以求出满条g?a', b')的个敎ͼ求和是l果。接下的问题是求a' + b' = n'的序对个数。可以认定gcd(a', n') = 1Q如果不是这L(fng)话,?x)有a' = t * A, n' = t * N, q样的话b' = t(N - A)Q这样就和a'、b'互素矛盾。这样只需要求和n'互素的数的个数即可,利用Ƨ拉函数可以很高效的找到满x(chng)件的序对个数?jin)?br>


sdfond 2010-04-26 23:34 发表评论
]]>
The Proof Of Euclid Algorithmhttp://m.shnenglu.com/sdfond/archive/2010/04/24/113465.htmlsdfondsdfondSat, 24 Apr 2010 13:29:00 GMThttp://m.shnenglu.com/sdfond/archive/2010/04/24/113465.htmlhttp://m.shnenglu.com/sdfond/comments/113465.htmlhttp://m.shnenglu.com/sdfond/archive/2010/04/24/113465.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/113465.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/113465.html  求gcd(a, b)Q欲证gcd(a, b) = gcd(b, a % b)
  设d1 = gcd(a, b), d2 = gcd(b, a % b), a > b且a = qb + r
  只需证d1 | d2q且d2 | d1?br>  因ؓ(f)d2 | b, d2 | r, 因此d2 | (b + qr) = aQ根据d2 | b且d2 | a有d2 | gcd(a, b)Q即d2 | d1?br>  因ؓ(f)d1 | a, d1 | b, 因此d1 | (a - qb) = rQ根据d1 | b且d1 | r有d1 | gcd(b, r)Q即d1 | d2?br>  Ҏ(gu)Euclid法执行q程Qgcd(a, b) = gcd(b, r) = gcd(r, b % r) = ... = gcd(rn, rn-1 % rn)Q如果rn-1 % rn = 0Q根据gcd(a, 0) = aQ有gcd(a, b) = rnQ证毕?br>
  貌似偏序关系上证a = b很多都是利用反对U性,比如a >= bq且b >= a则a = bQ或者a | bq且b | a则a = bQ一个很常用且强大的Ҏ(gu)?br>


sdfond 2010-04-24 21:29 发表评论
]]>
SPOJ 2154 Kruskalhttp://m.shnenglu.com/sdfond/archive/2010/02/06/107365.htmlsdfondsdfondSat, 06 Feb 2010 01:04:00 GMThttp://m.shnenglu.com/sdfond/archive/2010/02/06/107365.htmlhttp://m.shnenglu.com/sdfond/comments/107365.htmlhttp://m.shnenglu.com/sdfond/archive/2010/02/06/107365.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/107365.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/107365.html  猴子和Kruskal玩一个取矛_游戏Q给定n堆石子,n不大?00Q每堆石子的个数大于2于2 ^ 32Q双方轮?hu)取子,每次可以从一堆中取最多k个,当一方取完石子后某堆矛_的个数是素数的话那么当前玩家莯。问猴子是否有必胜策略?br>
【题目分析?br>  q是一道BT题,中间讄?jin)许多trick。开始对题意没有完全理解Q错?jin)很多次Q后来找来了(jin)数据Q才发现?jin)问题?br>  题目中描q的莯{略是:(x)"A player wins if after his move the size of some heap is a prime number."q句话乍一看以为是取完矛_后剩下的矛_个数是素数的时候就莯Q其实还隐藏着另一U可能:(x)如果多堆矛_个数是素敎ͼ当前玩家无论怎样取都能获胜,因ؓ(f)在他取完之后Q其他堆的石子个数是素数Q也满莯条g?br>  接下来考虑一般情c(din)这个题目是限制中间状态的Nim游戏Q也是_(d)对于一堆个Cؓ(f)n的石子而言Q它的SG值取决于于n的最大素数。注意这里题讑֏有了(jin)一个小trickQ题目说明了(jin)需要取1到k个,如果当前矛_个数本n是素敎ͼ当然是没用的Q因此是于n的最大素数。设于n的最大素数是p(题目中说明了(jin)矛_个数大于2Q因此p一定存?Q那么可以在k步以内到达p的一定是必胜态,而且是直接获胜,需要在输入的时候特?q一炚w要注意,在解决限制中间状态的Nim游戏时一般都需要特?。然后就是p + k + 1q个状态,因ؓ(f)它可辄状态全是必胜态,因此它是必|态,SGgؓ(f)0。现在考虑大于p + k + 1的状态,问题出来?jin)。以p + k + 2q个状态ؓ(f)例,因ؓ(f)它可以到达p + 2 ... p + k + 1q些状态,因ؓ(f)p + 2 ... p + k都是直接莯状态,如何判定他们的SG值呢Q如果假讑֮们的SG值是1Q那么p + k + 2q个状态的SG值应该是2。但是思考一下SG值的定义Q它是定义在一个DAG上的Q所有的状态最后都是可以在有限步内转移到终止?必|?。但是p + 1 ... p + kq些状态都转移C(jin)pq个状态上Q我们肯定不能认定p状态是l止态,因此仅仅凭借p + 1 ... p + kq些状态是必胜态就单的把它们的SGD?是不恰当的;q些限制状态和以前的题目还?sh)同Q这些限制状态都不能转移到终止态上Q但是由于题目的要求Q它们又都是必胜态,因此把它们的SGD为无I大更合适些?br>  仔细思考一下带限制状态的SG游戏Q可以发玎ͼ它们和一般的SG游戏的区别在于,在分析一般的SG游戏的时候,对于一个状态图而言Q{Udl止态的时候ƈ不意味着游戏l束Q因为玩家可以通过走其他的状态图来保证是否达到必胜态;但是带限制状态的SG游戏Q限制的状态双斚w是不敢走的,因ؓ(f)一旦一方走入限制状态,另一方立刻获胜,游戏q止了(jin)。可以认为,只要走入限制态就相当于认输,对于双方玩家而言肯定都不?x)这样做Q因此这些限制状态就成了(jin)“ȝ?#8221;Q完全可以忽视这些状态(也就是说不存在到q些状态的转移Q。通过上述分析Q我们可以认定p + k + 2q个状态的SG值应该是1而不??br>  现在q个问题的做法就比较明朗?jin),对于每堆矛_Q去掉限制态的讨论后,变成了(jin)在集合{1...k}中选择元素的一个Subtraction GameQ它的SG值是?k + 1)循环的?br>  然后是求最q素数的问题?sh)(jin),q个没有好办法,只能暴力枚D。对于一个数nQ在sqrt(n)到n之间存在素数Q感觉应该是Q但是不?x)证Q,因此最多枚举sqrt(n)ơ就能找到解。但是每ơ枚丑ֈ素的复杂度还是sqrt(n)Qd杂度q是比较高,我在本地跑数据跑?U,交上去超时了(jin)QSPOJ的时?0U,q居然也时Q数据够变态)(j)。想?jin)很久也没有什么新的算法,无奈只能在判素上下点功夫Q直接把Miller-Rabin搞出来了(jin)Q速度提高C(jin)2U,仍然时。后来又加了(jin)一些常C的优化,l于q了(jin)?br>
【ȝ?br>  q个题目做了(jin)很长旉Q一斚w是审题(sh)清,错了(jin)几次Q另一斚w是对于SG的理论理解的不够透彻Q想?jin)很久终于想明白了(jin);再有是优化法也花了(jin)很长时间。不q通过q个题目对于SG理论的理解又q了(jin)一步,感觉不错Q呵c(din)?

注:(x)本文作于2009q???9?7?br>


sdfond 2010-02-06 09:04 发表评论
]]>
UVa 3295 Counting Triangleshttp://m.shnenglu.com/sdfond/archive/2009/10/15/98712.htmlsdfondsdfondThu, 15 Oct 2009 11:54:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/10/15/98712.htmlhttp://m.shnenglu.com/sdfond/comments/98712.htmlhttp://m.shnenglu.com/sdfond/archive/2009/10/15/98712.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/98712.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/98712.html【题目大意?br>  m * n的区域内Q每个整数坐标都有整点,问以q些整点为端点能够Ş成多个三角形。(0 < m, n <= 1000Q?br>
【题目分析?br>  q个题目乍一看挺单的Q但是想做对q是要仔l的思考下的。补集{化的思想Q求出所有共U的三元l,然后用L减掉是{案?jin),关键是如何求共U三元组。x坐标相同和y坐标相同的比较好计算Q在一条斜U的׃好算?jin),M囑֏玎ͼ即斜率相同的线Q经q的格点数可能各不相同。思\当然q是枚Dy / xQ不同的y / x定?jin)不同的矩Ş区域Q,之后如何有效的计,我采用的Ҏ(gu)可能有些ȝ(ch)Q有点类Ҏ(gu)的方法。以斜率为a / bZQ?a, b) = gQ那么m * n的区域内一定有(m - a + 1) * (n - b + 1)个那么大的矩形,q样的矩形经q的格点数是(g + 1)Q然后因为同{斜率小一点的矩Ş(a - a / g, b - b / g)也是存在的,个数同样可以l计出来Q但是有些大矩Ş包括?jin),要去掉;因此采用这U思想Q先求出大矩形的个数Q然后依ơ往(xin)下减Q就可以避免重复计数?jin)。虽然这样复杂度有点高,不过极限数据q是比较快的跑出来了(jin)?br>  说的可能不太清楚Q具体代码如下:(x)
 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 const int N = 1024;
 5 
 6 bool tag[N][N];
 7 long long calc(long long n)
 8 {
 9     if (n <= 2)
10         return 0;
11     return n * (n - 1* (n - 2/ 6;
12 }
13 int gcd(int a, int b)
14 {
15     return b == 0 ? a : gcd(b, a % b);
16 }
17 
18 int main()
19 {
20     int m, n, g, a, b, timer = 1, cnt[N], ta, tb;
21     long long ans;
22 
23     while (scanf("%d %d"&m, &n) == 2)
24     {
25         memset(tag, 0sizeof(tag));
26         if (m == 0 && n == 0)
27             break;
28         ans = calc((m + 1* (n + 1)) - calc(m + 1* (n + 1- calc(n + 1* (m + 1);
29         for (int i = m; i >= 1; i--)
30             for (int j = n; j >= 1; j--)
31             {
32                 g = gcd(i, j);
33                 a = i / g, b = j / g;
34                 if (tag[a][b])      continue;
35                 memset(cnt, 0sizeof(cnt));
36                 tag[a][b] = 1;
37                 a = i, b = j;
38                 ta = i / g, tb = j / g;
39                 for (int k = g; k >= 2; k--)
40                 {
41                     cnt[k] += (m - a + 1* (n - b + 1);
42                     ans -= calc(k + 1* cnt[k] * 2;
43                     for (int t = 1; t <= k - 2; t++)
44                         cnt[k-t] -= (t + 1* cnt[k];
45                     a -= ta, b -= tb;
46                 }
47             }
48         cout << "Case " << timer++ << "" << ans << endl;
49     }
50 
51     return 0;
52 }
53 


sdfond 2009-10-15 19:54 发表评论
]]>
HOJ 1583http://m.shnenglu.com/sdfond/archive/2009/06/12/87466.htmlsdfondsdfondFri, 12 Jun 2009 03:15:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/06/12/87466.htmlhttp://m.shnenglu.com/sdfond/comments/87466.htmlhttp://m.shnenglu.com/sdfond/archive/2009/06/12/87466.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/87466.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/87466.html  首先考虑如果定nQ当n大于1的时候,n ^ n的位数是不同?n * logn)Q这h据输入的长度可以定n。之后就要考虑怎样(g)出q个数是不是正确的。因为只有一位可能有变换Q那么就是在原数的基上多?jin)(或少了(jin)?j)一个k * 10 ^ iQ其中k = 1...9Qi = 0...n。考察q个数的素因子,只可能是2???Q这L(fng)话如果我取一个模11Q显然k * 10 ^ i?1的g定不?Q这L(fng)话如果有一位发生了(jin)变化Q它?1的结果和n ^ n?1的结果肯定不同,Ҏ(gu)q个Ҏ(gu)我就可以在O(L)的复杂度内检出q个数是否正了(jin)QL是位数?br>  实现的时候有一个很Ҏ(gu)出错的地斏V因为需要预处理出每个n的nơ幂的位敎ͼ正常的话n * logn向上取整是{案Q但是n?0的整数幂的时候有些特别,是n * logn + 1Q需要单独处理(我是加了(jin)一?e-2再向上取_(d)(j)Q因个原因错?jin)一ơ,q有一ơ是输入的字W串大小开了(jin)?br>附题目代码:(x)
#include <cstdio>
#include 
<cmath>
#include 
<algorithm>
using namespace std;
const int MOD = 11, N = 100001;

int d[N], m[N];
int power_mod(int a, int b)
{
    
int ret = 1, f = a;
    
while (b)
    
{
        
if (b & 1)
            ret 
= ret * f % MOD;
        f 
= f * f % MOD;
        b 
>>= 1;
    }

    
return ret;
}

void init()
{
    
int tmp;
    
for (int i = 2; i < N; i++)
    
{
        tmp 
= (int)(ceil(i * log(i) / log(10.0+ 1e-2+ 1e-1);
        d[i] 
= tmp;
        m[i] 
= power_mod(i % MOD, i);
    }

}


int main()
{
    
char str[N*5];
    
int T, p, len, tmp;

    init();
    scanf(
"%d"&T);
    
while (T--)
    
{
        scanf(
"%s", str);
        len 
= strlen(str);
        
if (len == 1 && str[0== '1')
        
{
            puts(
"1");
            
continue;
        }

        p 
= lower_bound(d, d + N, len) - d;
        tmp 
= 0;
        
for (int i = 0; i < len; i++)
        
{
            tmp 
= tmp * 10 + (str[i] - '0');
            tmp 
= tmp % MOD;
        }

        printf(
"%d\n", tmp == m[p] ? p : -1);
    }


    
return 0;
}



sdfond 2009-06-12 11:15 发表评论
]]>
PKU 3696http://m.shnenglu.com/sdfond/archive/2009/06/10/87270.htmlsdfondsdfondWed, 10 Jun 2009 01:16:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/06/10/87270.htmlhttp://m.shnenglu.com/sdfond/comments/87270.htmlhttp://m.shnenglu.com/sdfond/archive/2009/06/10/87270.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/87270.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/87270.html  题目大意是给定一个LQL不大?000000000Q,求最长度的能整除L的都?的数Q输出长度,如果没有输出0。设长度为n的话Q那么这个全?的数可以表示? + 80 + 800 + ...是一个等比数列,变换后得? * (10 ^ n - 1) / 9 = k * L。如果L?的因子数不大?个,那么可以除掉。之后等式可以写?0 ^ n - 1 = 9 * k * LQ也可以{换成一个模方程Q?0 ^ n = 1 mod 9L。根据欧拉定理,一定要10? * L互素才能有解Q这样就可以判定出无解的情况?jin)。之后相当于求一个原根,求出9L的欧拉函敎ͼ讑օ为phiQ那么n一定是phi的约数才可以满条gQ枚举phi的约数就可以?jin)?br>  q个题目变态的一个地方在?L和phi都可能很大,int表示不下Q要用long longQ这么大的模单的乘法?x)溢出,也要写成二分的Ş式。中间有好几个地Ҏ(gu)都用?jin)intQ结果导致溢出,时?ơ。ȝ来说q是个很好的数论题目Q以后这U题目应该多l习(fn)一些?img src ="http://m.shnenglu.com/sdfond/aggbug/87270.html" width = "1" height = "1" />

sdfond 2009-06-10 09:16 发表评论
]]>
PKU 1305http://m.shnenglu.com/sdfond/archive/2009/06/09/87136.htmlsdfondsdfondTue, 09 Jun 2009 01:09:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/06/09/87136.htmlhttp://m.shnenglu.com/sdfond/comments/87136.htmlhttp://m.shnenglu.com/sdfond/archive/2009/06/09/87136.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/87136.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/87136.html  是个l典问题?sh)(jin),求满a ^ 2 + b ^ 2 = c ^ 2且a、b、c两两互质的三元组个数Q其中a、b、c都小于等于nQ同时还要求出n以内不属于三元组Q不必互质)(j)的数的个敎ͼ其中n <= 1000000?br />  数据范围很大Q单U的n ^ 2的枚举肯定是不行的。我发现互质的三元组不多Q想找一个办法求Z质的Q然后筛出其他的三元l(乘(sh)倍数Q,但是仍然想不出怎样很快扑ֈ互质的三元组。到|上查了(jin)一下,扑ֈ?jin)一个很好的|站Q?a >http://xserve.math.nctu.edu.tw/people/cpai/carnival/fraction/05.htmQ讲解这个问题?br />  首先只需扑ֈ互质的a和b可以了(jin)Q其余的可以通过乘(sh)一定的倍数得到。首先a和b必须不能都是奇数Q否则的话,不妨设a = 2p + 1, b = 2q + 1, c = 2r。带入a ^ 2 + b ^ 2 = c ^ 2有:(x)2 * (r ^ 2 - p ^ 2 - q ^ 2 - p - q) = 1Q出C(jin)矛盾。这样必然a和b一奇一Ӟ设a是偶数。恒{变换一下:(x)Q这里是x ^ 2 + y ^ 2 = z ^ 2Qx是偶敎ͼ(j)


  令u = (z - y) / 2Qv = (z + y) / 2。因为y、z互质Q那么u、v互质。如果不互质Q有v = kuQ变换后得到Qz / y = (k + 1) / (k - 1)。因为z和y互质Q这栯个等式的解只能是z = k + 1且y = k - 1。这L(fng)话u = 1。可以理解ؓ(f)q也是互质的Ҏ(gu)情况(gcd = 1)。这?x / 2) ^ 2 = u * vQu和v没有公共的质因数Q因此必然u和v都是完全qx(chng)数。接下来的事情就比较单了(jin)Qo(h)u = m ^ 2Qv = n ^ 2Q然后利用m和n表示x、y、z有:(x)y = n ^ 2 - m ^ 2Qx = 2 * m * nQz = n ^ 2 + m ^ 2。这样题目中数据范围1000000Q而枚举m和n的范围最?000Q这个复杂度可以接受了(jin)。枚丄时候不用担?j)出现重复的互质的x与yQ因里x一定是偶数Q如果x和y已经互质Q那么y一定是奇数Q解是唯一的?br />  很囧的是q个巧妙的方法居然公元前250q就有大牛想C(jin)Q丢番图Q,实在orzQ?

 



sdfond 2009-06-09 09:09 发表评论
]]>
完美数问?/title><link>http://m.shnenglu.com/sdfond/archive/2009/06/04/86778.html</link><dc:creator>sdfond</dc:creator><author>sdfond</author><pubDate>Thu, 04 Jun 2009 11:57:00 GMT</pubDate><guid>http://m.shnenglu.com/sdfond/archive/2009/06/04/86778.html</guid><wfw:comment>http://m.shnenglu.com/sdfond/comments/86778.html</wfw:comment><comments>http://m.shnenglu.com/sdfond/archive/2009/06/04/86778.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/sdfond/comments/commentRss/86778.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/sdfond/services/trackbacks/86778.html</trackback:ping><description><![CDATA[  定义一个正整数n的函数F(n)为n的每位数的乘U。一个数是good如果F(n)不ؓ(f)0q且n能整除F(n)Q一个数是完数当且仅当n是goodq且n+1也是good。现在问一个长度是k位的C有多个完美敎ͼ其中k不大?000000?br>  不得不承认这是一个非常经典的ȝ分析问题能力的题目。设n = a1a2...anQ因为F(n)不ؓ(f)0Q所以n + 1 = a1a2...(an+1)。根据题目意思,我们可以列出{式Qn + 1 = q * (a1 * a2 * ... * (an + 1))Q又n = p * (a1 * a2 * ... * an)。o(h)A = a1 * a2 ... * an-1Q整理前面的{式可以得到Q?q * an + q - an) * A = 1。因里面都是整数Q显然A = 1Q也是说a1Qa2Q?..Qan-1都是1。问题(sh)下子变得简化了(jin)很多Q比起之前的单纯枚D可以说是q了(jin)一大步。但是如果枚举最后一位判断是不是可行复杂度依然很高,需要进一步讨论。首先an???对应的数都是goodQan?的时候,如果是good必须前面n-1?加和?的倍数Qan?的时候需要最后两位被4整除才行Q?4不满x(chng)Ӟ同理an?的时候也必须前面n-1?加和?的倍数Qan?的时候需要后三位的数?整除Q显然也不可能;l过一番讨论,剩7q个特别的数字了(jin)。用一??试除一下发C个规律,111111恰好整除7Q六个数一循环。这样只需判断(an - 1)是否整除6可以了(jin)?br>  lg所qͼ对于一个k位的数字Q如果k?Q那么结果是8。如果k大于1Q结果首先是1Q如果k-1能被3整除Q结果加2Q如果k-1能被6整除Q结果再?。这样O(1)的时间复杂度出l果?jin),仔细分析问题后得出的做法真强大啊?img src ="http://m.shnenglu.com/sdfond/aggbug/86778.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/sdfond/" target="_blank">sdfond</a> 2009-06-04 19:57 <a href="http://m.shnenglu.com/sdfond/archive/2009/06/04/86778.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PKU 1811http://m.shnenglu.com/sdfond/archive/2009/04/03/78864.htmlsdfondsdfondFri, 03 Apr 2009 12:06:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/04/03/78864.htmlhttp://m.shnenglu.com/sdfond/comments/78864.htmlhttp://m.shnenglu.com/sdfond/archive/2009/04/03/78864.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/78864.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/78864.html  大整数的快速质因子分解Q用到pollard-rho启发式算法?br>  法D上介lpollard-rho介绍得比较详l,׃因子的循环节长度很,通过倍增步长Qpollard-rho能够很快的找C个大整数的一个较?yu)的素因子pQ书中说复杂度在O(sqrt(p))内,用的什么概率的分析Ҏ(gu)Q不懂。实际中pollard-rho的速度q是很快的,当然可能出现d@环?br>  q个题目要利用pollard-rho扑ֈ一个数的最素因子Q因此还需要Miller-Rabin试来辅助。原来写的那个Miller-Rabin很快挂掉?jin),因?f)没有用到二次探测Q判不出来Carmichael数。如果x ^ 2 = 1 (mod n)Q如果n是质敎ͼ那么x只能?和n - 1Q二ơ探就是利用这个定理来q行(g)?br>  POJ的论坛里面更有牛人列Z(jin)N多Carmichael敎ͼ真不知道他怎么扑ֈ的。最初怎么也不知道二次探测加在哪里好,后来参考网上一位大牛的代码Q它的方法是计算a ^ b % n的时候,先将b折半C个奇数b'为止Q计a ^ b'Q然后倍增b'Q同时进行二ơ检,x(chng)觉得很有道理Q因为如果x ^ 2 = 1 mod n成立的话Q那?x ^ 2) ^ 2 = 1 mod n也成立?br>  q个题目q有一个trick是模能辑ֈ2 ^ 54Q如果这栯一个数qx(chng)的时候,即long long也会(x)溢出。后来发现可以用快速幂取模的思想弄个"快速积取模"Q同样将b表示成二q制的Ş式,倍增的同时加到结果上p?jin)。这hơ运的数范围都? ^ 54以内Qƈ且都是加操作Q不?x)溢Z(jin)?br>  POJ上还有一个用pollard-rho做的题是PKU 2429Q这个比Prime Testq恶Q因个题目是d底底q行factorizationQ而且之后q要枚D一下找最优解QM我的代码非常的长Q而且q个题目数据范围2 ^ 63Q必ȝunsigned long long才能q。我的代码中间出C(jin)一些减操作Q都要特D处理一下。最后还犯了(jin)个低U错误,函数q回值写错了(jin)Q找?jin)好几遍才找出来?br>
附PKU 1811代码Q?/p>
PKU 1811


sdfond 2009-04-03 20:06 发表评论
]]>
HOJ 1377 - U数问题http://m.shnenglu.com/sdfond/archive/2009/03/30/78412.htmlsdfondsdfondMon, 30 Mar 2009 13:44:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/03/30/78412.htmlhttp://m.shnenglu.com/sdfond/comments/78412.htmlhttp://m.shnenglu.com/sdfond/archive/2009/03/30/78412.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/78412.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/78412.htmlq是一个经兔R题(sh)(jin)Q我一直以Z(x)有经典算法,开始的时候一直往(xin)贪心(j)上想Q结果owenl出?jin)反例。后来经q吉大牛Ҏ(gu)Q因为k <= 10 ^ 15Q可以根据这个定界,最差情况k的素因子也不?x)超q?3Q这样就可以搜烦(ch)?jin)?br>实现的时候我也犯?jin)几个小错,一个是?0 ^ 15打?jin)一?Q还有一个剪枝必dQ如果当前结果的U数个数为fQ那么如果n % f不ؓ(f)0Q则剪掉Q因为约C数是以乘U的关系累加的?br>
 1 #include <cstdio>
 2 const int M = 14;
 3 const long long max = 1000000000000000LL;
 4 
 5 int p[M] = {2357111317192329313741}, k;
 6 long long ans;
 7 void solve(long long v, int factor, int pos)
 8 {
 9     if (factor >= k)
10     {
11         if (factor == k)    ans <?= v;
12         return;
13     }
14     if (k % factor) return;
15     if (pos == M)   return;
16     for (int i = 1; i <= 50; i++)
17     {
18         v *= p[pos];
19         if (v > max)    break;
20         solve(v, factor * (i + 1), pos + 1);
21     }
22 }
23 
24 int main()
25 {
26     while (scanf("%d"&k) == 1)
27     {
28         ans = max + 1;
29         solve(110);
30         if (ans > max)   printf("-1\n");
31         else             printf("%lld\n", ans);
32     }
33 
34     return 0;
35 }
36 


sdfond 2009-03-30 21:44 发表评论
]]>
NQ的最末位非零?/title><link>http://m.shnenglu.com/sdfond/archive/2009/03/29/78298.html</link><dc:creator>sdfond</dc:creator><author>sdfond</author><pubDate>Sun, 29 Mar 2009 13:00:00 GMT</pubDate><guid>http://m.shnenglu.com/sdfond/archive/2009/03/29/78298.html</guid><wfw:comment>http://m.shnenglu.com/sdfond/comments/78298.html</wfw:comment><comments>http://m.shnenglu.com/sdfond/archive/2009/03/29/78298.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://m.shnenglu.com/sdfond/comments/commentRss/78298.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/sdfond/services/trackbacks/78298.html</trackback:ping><description><![CDATA[  l定一个数NQN <= 10 ^ 1000Q,如何快速求得NQ的最末位非零数是一个经典的问题。一直以来都被这个问题困扎ͼ今天仔细想了(jin)下,l于l想通了(jin)Q尽可能有些笨拙,现把x(chng)记录于此?br>  在N很小的情况下Q有一个简便的Ҏ(gu)Q求?到N之间每个数的2的因子数?的因子数Q记为F(2)和F(5)Q显然F(2) >= F(5)。由于在末尾只有2?怹才能产生0Q如果我们把2?抛去Q那么肯定不?x)?Q这样就可以一边乘?sh)Ҏ(gu)10Q防止溢出。剩下的一??如何处理呢?因ؓ(f)2肯定?多,因此最末位肯定是偶敎ͼ0的阶乘和1的阶乘除外)(j)。而一个偶C停地?Q最末位的规律是Q? -> 4 -> 8 -> 6 -> 2 -> ...出现??循环Q这h们先用F(2) - F(5)Q得一部分2?匚w上,2 * 5 = 10Q对末尾不生媄(jing)响,剩下?模一?Q剩几再乘几?可以了(jin)?br>  但是q个Ҏ(gu)在N非常大的时候肯定就不行?jin),但是可以利用扑@环这个思想l箋(hu)做。如果算阶乘的时候蟩q?的倍数Q记G(n)q?的倍数的时候,?乘到n的最末非零位Q也是?的倍数?乘。可以发玎ͼ(x)<br>G(1) = 1, G(2) = 2, G(3) = 6, G(4) = 4, G(5) = 4, G(6) = 4, G(7) = 8, G(8) = 4, G(9) = 6, G(10) = 6, G(11) = 6, G(12) = 2, G(13) = 6...<br>  又出C(jin)循环Q每10个数循环一ơ。如何计G(n)变的很单,求出n的最末位Q就知道对应的G(n)是多了(jin)Q当焉要特判n = 1的情c(din)由于我们把5的倍数的数都提出来?jin),提出来的q些敎ͼ5?0?5?0?5?0...Q每个除?后又l成?jin)一个阶乘序列!除完5一共提Z(jin)n / 5?Q根据之前的分析Q每?都可以拿Z?和它配对然后把它消去Q这样一?q当于一?Q我们就要把原来的数乘(sh)3?Q模四@环)(j)。这样一?的个数其实也可以模四Q模完四之后剩k的话Q就可以乘(sh)k?Q就把所有的5消去?jin)。现在ȝ一下:(x)对一个数n的阶乘,计算它的末尾非零位,先计G(n)Q相当于?的倍数的数的乘U最末非零位先算好了(jin)Q然后乘?sh)n / 5 % 4?Q处理了(jin)提出的n / 5?Q这样之后还剩下n / 5的阶乘没有算。递归的求解n / 5的阶乘的最末位非零敎ͼ再乘?sh)去得到结果?jin)?br>  q个做法的复杂度很低了(jin)Q达到O(log n)Q对?0 ^ 1000的数据,利用高精度做p?jin)。利用这U@环的思想Q算排列数P(n, k)的最末非零数也就可以做到?jin)?br>附HOJ 1013代码Q?br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #008080;"> 1</span><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">cstdio</span><span style="color: #000000;">></span><span style="color: #000000;"><br></span><span style="color: #008080;"> 2</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">cstring</span><span style="color: #000000;">></span><span style="color: #000000;"><br></span><span style="color: #008080;"> 3</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">const</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> N </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">1024</span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 4</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img id="Codehighlighter1_73_102_Open_Image" onclick="this.style.display='none'; codehighlighter1_73_102_open_text.style.display=" none="" ;="" codehighlighter1_73_102_closed_image.style.display="'inline';" codehighlighter1_73_102_closed_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_73_102_Closed_Image" style="display: none;" onclick="this.style.display='none'; codehighlighter1_73_102_closed_text.style.display=" none="" ;="" codehighlighter1_73_102_open_image.style.display="'inline';" codehighlighter1_73_102_open_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> hash[</span><span style="color: #000000;">10</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span id="Codehighlighter1_73_102_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id="Codehighlighter1_73_102_Open_Text"><span style="color: #000000;">{</span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">}</span></span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 6</span><span style="color: #000000;"><img id="Codehighlighter1_130_159_Open_Image" onclick="this.style.display='none'; codehighlighter1_130_159_open_text.style.display=" none="" ;="" codehighlighter1_130_159_closed_image.style.display="'inline';" codehighlighter1_130_159_closed_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_130_159_Closed_Image" style="display: none;" onclick="this.style.display='none'; codehighlighter1_130_159_closed_text.style.display=" none="" ;="" codehighlighter1_130_159_open_image.style.display="'inline';" codehighlighter1_130_159_open_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> one_digit_hash[</span><span style="color: #000000;">10</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span id="Codehighlighter1_130_159_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id="Codehighlighter1_130_159_Open_Text"><span style="color: #000000;">{</span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">}</span></span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 7</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;"> 8</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> last_digit(</span><span style="color: #0000ff;">char</span><span style="color: #000000;"> str[N], </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> st, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> to)<br></span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img id="Codehighlighter1_207_712_Open_Image" onclick="this.style.display='none'; codehighlighter1_207_712_open_text.style.display=" none="" ;="" codehighlighter1_207_712_closed_image.style.display="'inline';" codehighlighter1_207_712_closed_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_207_712_Closed_Image" style="display: none;" onclick="this.style.display='none'; codehighlighter1_207_712_closed_text.style.display=" none="" ;="" codehighlighter1_207_712_open_image.style.display="'inline';" codehighlighter1_207_712_open_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_207_712_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id="Codehighlighter1_207_712_Open_Text"><span style="color: #000000;">{<br></span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i, tmp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">, ret, num_of_five </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top"><br></span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (st </span><span style="color: #000000;">==</span><span style="color: #000000;"> to)<br></span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> one_digit_hash[str[st]</span><span style="color: #000000;">-</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">];<br></span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top"><br></span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    ret </span><span style="color: #000000;">=</span><span style="color: #000000;"> hash[str[to]</span><span style="color: #000000;">-</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">];<br></span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (i </span><span style="color: #000000;">=</span><span style="color: #000000;"> st; i </span><span style="color: #000000;"><=</span><span style="color: #000000;"> to; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">17</span><span style="color: #000000;"><img id="Codehighlighter1_379_535_Open_Image" onclick="this.style.display='none'; codehighlighter1_379_535_open_text.style.display=" none="" ;="" codehighlighter1_379_535_closed_image.style.display="'inline';" codehighlighter1_379_535_closed_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_379_535_Closed_Image" style="display: none;" onclick="this.style.display='none'; codehighlighter1_379_535_closed_text.style.display=" none="" ;="" codehighlighter1_379_535_open_image.style.display="'inline';" codehighlighter1_379_535_open_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">    </span><span id="Codehighlighter1_379_535_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id="Codehighlighter1_379_535_Open_Text"><span style="color: #000000;">{<br></span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">        tmp </span><span style="color: #000000;">=</span><span style="color: #000000;"> tmp </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #000000;">10</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> str[i] </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">        str[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> tmp </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #000000;">5</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">        tmp </span><span style="color: #000000;">%=</span><span style="color: #000000;"> </span><span style="color: #000000;">5</span><span style="color: #000000;">;<br></span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">        num_of_five </span><span style="color: #000000;">=</span><span style="color: #000000;"> (num_of_five </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #000000;">10</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> str[i] </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">) </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #000000;">4</span><span style="color: #000000;">;<br></span><span style="color: #008080;">22</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">    }</span></span><span style="color: #000000;"><br></span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (str[st] </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">)  st</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">24</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    ret </span><span style="color: #000000;">=</span><span style="color: #000000;"> last_digit(str, st, to) </span><span style="color: #000000;">*</span><span style="color: #000000;"> ret </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #000000;">10</span><span style="color: #000000;">;<br></span><span style="color: #008080;">25</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (num_of_five</span><span style="color: #000000;">--</span><span style="color: #000000;">)   ret </span><span style="color: #000000;">=</span><span style="color: #000000;"> ret </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;"> </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #000000;">10</span><span style="color: #000000;">;     </span><span style="color: #008000;">//</span><span style="color: #008000;">mul one 5 equals mul one 8</span><span style="color: #008000;"><br></span><span style="color: #008080;">26</span><span style="color: #008000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top"></span><span style="color: #000000;"><br></span><span style="color: #008080;">27</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ret;<br></span><span style="color: #008080;">28</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span><span style="color: #000000;"><br></span><span style="color: #008080;">29</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: #008080;">30</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main()<br></span><span style="color: #008080;">31</span><span style="color: #000000;"><img id="Codehighlighter1_726_857_Open_Image" onclick="this.style.display='none'; codehighlighter1_726_857_open_text.style.display=" none="" ;="" codehighlighter1_726_857_closed_image.style.display="'inline';" codehighlighter1_726_857_closed_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_726_857_Closed_Image" style="display: none;" onclick="this.style.display='none'; codehighlighter1_726_857_closed_text.style.display=" none="" ;="" codehighlighter1_726_857_open_image.style.display="'inline';" codehighlighter1_726_857_open_text.style.display="'inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_726_857_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id="Codehighlighter1_726_857_Open_Text"><span style="color: #000000;">{<br></span><span style="color: #008080;">32</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> str[N];<br></span><span style="color: #008080;">33</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top"><br></span><span style="color: #008080;">34</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%s</span><span style="color: #000000;">"</span><span style="color: #000000;">, str) </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">)<br></span><span style="color: #008080;">35</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">        printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">, last_digit(str, </span><span style="color: #000000;">0</span><span style="color: #000000;">, strlen(str) </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">));<br></span><span style="color: #008080;">36</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top"><br></span><span style="color: #008080;">37</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">38</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span><span style="color: #000000;"><br></span><span style="color: #008080;">39</span><span style="color: #000000;"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"></span></div><img src ="http://m.shnenglu.com/sdfond/aggbug/78298.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/sdfond/" target="_blank">sdfond</a> 2009-03-29 21:00 <a href="http://m.shnenglu.com/sdfond/archive/2009/03/29/78298.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一Ҏ(gu)\http://m.shnenglu.com/sdfond/archive/2009/03/28/78142.htmlsdfondsdfondSat, 28 Mar 2009 07:02:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/03/28/78142.htmlhttp://m.shnenglu.com/sdfond/comments/78142.htmlhttp://m.shnenglu.com/sdfond/archive/2009/03/28/78142.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/78142.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/78142.html最q又研究?jin)线性筛素数Ҏ(gu)的扩展,的确非常强大?br>l典的应用是U性时间筛Ƨ拉函数和求U数个数。我想了(jin)一下线性时间筛U数?U性函?Q也是可行的?br>对于一个大?的数nQ可以写成p1 ^ a1 * p2 ^ a2 * ... * pn ^ anQ那么n的约数和是(p1 ^ 0 + p1 ^ 1 + ... p1 ^ a1) * ... * (pn ^ 0 + ... + pn ^ an)Q由此就可以有递推关系?jin)?x)
设f[i]表示i的约数和Qe[i]表示i的最素因子个数Qt[i]表示(p1 ^ 0 + .. + p1 ^ a1)Qp1是t的最素因子Qa1是p1的幂ơ,q样对于i * p[j]Q如果p[j]不是i的因子,那么Ҏ(gu)U性条Ӟf[i*p[j]] = f[i] * (1 + p[j])Qe[i] = 1Qt[i] = 1 + p[j]Q如果p[j]是i的因子,那么相当于t[i]多了(jin)一p1 ^ (a1 + 1)Q首先e[i]++Q然后tmp = t[i]Qt[i] += p[j] ^ e[i]Qf[i*p[j]] = f[i] / tmp * t[i]。这样也做C(jin)O(1)的时间计出?jin)f[i*p[j]]同时也计出?jin)附加信息?br>
q种Ҏ(gu)q可以(h)l推q,例如可以记录i的最素因子Q这样就可以做到O(log n)旉的素因子分解?/p>

sdfond 2009-03-28 15:02 发表评论
]]>
TJU 3027 模线性方E组http://m.shnenglu.com/sdfond/archive/2009/03/22/77459.htmlsdfondsdfondSun, 22 Mar 2009 01:51:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/03/22/77459.htmlhttp://m.shnenglu.com/sdfond/comments/77459.htmlhttp://m.shnenglu.com/sdfond/archive/2009/03/22/77459.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/77459.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/77459.html
TJU 3027


sdfond 2009-03-22 09:51 发表评论
]]>
HOJ 1356 —?Miller Rabin试http://m.shnenglu.com/sdfond/archive/2009/03/18/77069.htmlsdfondsdfondWed, 18 Mar 2009 13:15:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/03/18/77069.htmlhttp://m.shnenglu.com/sdfond/comments/77069.htmlhttp://m.shnenglu.com/sdfond/archive/2009/03/18/77069.html#Feedback0http://m.shnenglu.com/sdfond/comments/commentRss/77069.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/77069.htmlMiller-RabinZ贚w定理:(x)如果(a, p) = 1Q那么a ^ (p - 1) = 1 mod p。满个性质的p叫伪素数Q如果一个数是伪素数Q那么它有很大可能是素数。通过多次的枚举aQ利用快速幂取模判断Q就可以知道p是不是素敎ͼMiller-Rabin试的成功率?/4?br>贚w定理是该定理的Ҏ(gu)形式Q如果p是素敎ͼ那么对于L整数aQa ^ p = a mod p。这个定理可以用归纳法证明,证明依据q样一个事实:(x)l合数C(n, k)是一个整敎ͼ如果n是素敎ͼ那么n和k!?n - k)!的每一w互素Q可以提出nQ也是C(n, k) / n也是整数Q所以n | C(n, k)?br>q个题目的代码如下:(x)
#include <cstdio>
#include 
<stdlib.h>
const int MAX = 4, N = 1000000;

long long PowerMod(long long a, long long b, long long k)
{
    
long long ret = 1, f = a;

    
while (b)
    
{
        
if (b & 1)
            ret 
= ret * f % k;
        f 
= f * f % k;
        b 
>>= 1;
    }

    
return ret;
}

bool MillerRabin(long long n)
{
    
int i;
    
long long tmp;

    srand(
100);
    
for (i = 0; i < MAX; i++)
    
{
        tmp 
= rand() % (n - 1+ 1;
        
if (PowerMod(tmp, n - 1, n) != 1)
            
break;
    }

    
return (i == MAX);
}


int main()
{
    
long long n, i, j;
    
bool tag[N] = {110};

    
for (i = 2; i * i < N; i++)
    
{
        
if (tag[i]) continue;
        
for (j = i; j * i < N; j++)
            tag[j
*i] = 1;
    }

    
while (scanf("%lld"&n) == 1)
    
{
        
if (n < N)
            printf(
"%s\n", tag[n] ? "NO" : "YES");
        
else
            printf(
"%s\n", ((n & 1== 0|| !MillerRabin(n) ? "NO" : "YES");
    }


    
return 0;
}



sdfond 2009-03-18 21:15 发表评论
]]>
NKU 1026 ——模U性方E?/title><link>http://m.shnenglu.com/sdfond/archive/2009/03/17/76905.html</link><dc:creator>sdfond</dc:creator><author>sdfond</author><pubDate>Tue, 17 Mar 2009 12:16:00 GMT</pubDate><guid>http://m.shnenglu.com/sdfond/archive/2009/03/17/76905.html</guid><wfw:comment>http://m.shnenglu.com/sdfond/comments/76905.html</wfw:comment><comments>http://m.shnenglu.com/sdfond/archive/2009/03/17/76905.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/sdfond/comments/commentRss/76905.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/sdfond/services/trackbacks/76905.html</trackback:ping><description><![CDATA[<p>也是很赤裸裸的模型,q里求的是绝对值最解Q还有就是用到高_。我用java不会(x)写传引用Q因此只好开?jin)全局变量?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.math.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.util.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Main<br><img id=Codehighlighter1_59_1147_Open_Image onclick="this.style.display='none'; Codehighlighter1_59_1147_Open_Text.style.display='none'; Codehighlighter1_59_1147_Closed_Image.style.display='inline'; Codehighlighter1_59_1147_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_59_1147_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_59_1147_Closed_Text.style.display='none'; Codehighlighter1_59_1147_Open_Image.style.display='inline'; Codehighlighter1_59_1147_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_59_1147_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_59_1147_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>  </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> BigInteger x </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">, y </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>  </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> BigInteger extended_gcd(BigInteger a, BigInteger b)<br><img id=Codehighlighter1_173_492_Open_Image onclick="this.style.display='none'; Codehighlighter1_173_492_Open_Text.style.display='none'; Codehighlighter1_173_492_Closed_Image.style.display='inline'; Codehighlighter1_173_492_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_173_492_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_173_492_Closed_Text.style.display='none'; Codehighlighter1_173_492_Open_Image.style.display='inline'; Codehighlighter1_173_492_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>  </span><span id=Codehighlighter1_173_492_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_173_492_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      BigInteger zero </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BigInteger(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> String(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      BigInteger ret, tmp;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (b.compareTo(zero) </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_291_365_Open_Image onclick="this.style.display='none'; Codehighlighter1_291_365_Open_Text.style.display='none'; Codehighlighter1_291_365_Closed_Image.style.display='inline'; Codehighlighter1_291_365_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_291_365_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_291_365_Closed_Text.style.display='none'; Codehighlighter1_291_365_Open_Image.style.display='inline'; Codehighlighter1_291_365_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>      </span><span id=Codehighlighter1_291_365_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_291_365_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          x </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BigInteger(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> String(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          y </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> zero;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> a;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>      }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      ret </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> extended_gcd(b, a.mod(b));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      tmp </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> x;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      x </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> y;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      y </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tmp.subtract(a.divide(b).multiply(y));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> ret;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>  }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>  <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>  </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> BigInteger modular_linear_equation(BigInteger a, BigInteger b, BigInteger n)<br><img id=Codehighlighter1_585_792_Open_Image onclick="this.style.display='none'; Codehighlighter1_585_792_Open_Text.style.display='none'; Codehighlighter1_585_792_Closed_Image.style.display='inline'; Codehighlighter1_585_792_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_585_792_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_585_792_Closed_Text.style.display='none'; Codehighlighter1_585_792_Open_Image.style.display='inline'; Codehighlighter1_585_792_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>  </span><span id=Codehighlighter1_585_792_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_585_792_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      BigInteger e, e2;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      BigInteger d </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> extended_gcd(a, n);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      e </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> b.divide(d).multiply(x).mod(n.divide(d));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      e2 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> e.subtract(n.divide(d)).abs();<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (e.compareTo(e2) </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> e;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> e2;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>  }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>  <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>  </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> main(String[] args)<br><img id=Codehighlighter1_840_1145_Open_Image onclick="this.style.display='none'; Codehighlighter1_840_1145_Open_Text.style.display='none'; Codehighlighter1_840_1145_Closed_Image.style.display='inline'; Codehighlighter1_840_1145_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_840_1145_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_840_1145_Closed_Text.style.display='none'; Codehighlighter1_840_1145_Open_Image.style.display='inline'; Codehighlighter1_840_1145_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>  </span><span id=Codehighlighter1_840_1145_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_840_1145_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      BigInteger a, b, c;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      Scanner in </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Scanner(System.in);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    <br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>      </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> (in.hasNext())<br><img id=Codehighlighter1_937_1141_Open_Image onclick="this.style.display='none'; Codehighlighter1_937_1141_Open_Text.style.display='none'; Codehighlighter1_937_1141_Closed_Image.style.display='inline'; Codehighlighter1_937_1141_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_937_1141_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_937_1141_Closed_Text.style.display='none'; Codehighlighter1_937_1141_Open_Image.style.display='inline'; Codehighlighter1_937_1141_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>      </span><span id=Codehighlighter1_937_1141_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_937_1141_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          a </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> in.nextBigInteger();<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          b </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> in.nextBigInteger();<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> in.nextBigInteger();<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.multiply(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BigInteger(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> String(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">-1</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>          System.out.println(modular_linear_equation(a, c, b));<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>      }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>  }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div> <img src ="http://m.shnenglu.com/sdfond/aggbug/76905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/sdfond/" target="_blank">sdfond</a> 2009-03-17 20:16 <a href="http://m.shnenglu.com/sdfond/archive/2009/03/17/76905.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ 2115 —?模线性方E?/title><link>http://m.shnenglu.com/sdfond/archive/2009/03/17/76893.html</link><dc:creator>sdfond</dc:creator><author>sdfond</author><pubDate>Tue, 17 Mar 2009 10:53:00 GMT</pubDate><guid>http://m.shnenglu.com/sdfond/archive/2009/03/17/76893.html</guid><wfw:comment>http://m.shnenglu.com/sdfond/comments/76893.html</wfw:comment><comments>http://m.shnenglu.com/sdfond/archive/2009/03/17/76893.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://m.shnenglu.com/sdfond/comments/commentRss/76893.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/sdfond/services/trackbacks/76893.html</trackback:ping><description><![CDATA[最后化成c * x = b - a mod (2 ^ k)Q解q个模线性方E,输出最正解即可?br>写程序的时候有?jin)一个误区,以ؓ(f)如果b - a是负的,把它化成正的话那么输出的时候就可以直接? ^ kQ不用再考虑是负的情况了(jin)。但是忽略了(jin)x可能的情况,所以WA?jin)很多次。其实根本不需考虑b - a的正负性,最后输出的时候加2 ^ k再模2 ^ kp?jin)?br>q有一个是输出最解Q因为最后的所有解模n / d同余Q因此直接模n / d卛_?br> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">cstdio</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">ax + by = gcd(a, b)</span><span style="COLOR: #008000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> extended_gcd(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> a, </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> b, </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">x, </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">y)<br><img id=Codehighlighter1_118_313_Open_Image onclick="this.style.display='none'; Codehighlighter1_118_313_Open_Text.style.display='none'; Codehighlighter1_118_313_Closed_Image.style.display='inline'; Codehighlighter1_118_313_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_118_313_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_118_313_Closed_Text.style.display='none'; Codehighlighter1_118_313_Open_Image.style.display='inline'; Codehighlighter1_118_313_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_118_313_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_118_313_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> ret, tmp;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">b)<br><img id=Codehighlighter1_160_206_Open_Image onclick="this.style.display='none'; Codehighlighter1_160_206_Open_Text.style.display='none'; Codehighlighter1_160_206_Closed_Image.style.display='inline'; Codehighlighter1_160_206_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_160_206_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_160_206_Closed_Text.style.display='none'; Codehighlighter1_160_206_Open_Image.style.display='inline'; Codehighlighter1_160_206_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_160_206_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_160_206_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        x </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">, y </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> a;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    ret </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> extended_gcd(b, a </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> b, x, y);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    tmp </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> x;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    x </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> y;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    y </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tmp </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> a </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> b </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> y;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> ret;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">ax = b mod n</span><span style="COLOR: #008000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> modular_linear_equation(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> a, </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> b, </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> n)<br><img id=Codehighlighter1_404_551_Open_Image onclick="this.style.display='none'; Codehighlighter1_404_551_Open_Text.style.display='none'; Codehighlighter1_404_551_Closed_Image.style.display='inline'; Codehighlighter1_404_551_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_404_551_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_404_551_Closed_Text.style.display='none'; Codehighlighter1_404_551_Open_Image.style.display='inline'; Codehighlighter1_404_551_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_404_551_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_404_551_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> x, y, e;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> d </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> extended_gcd(a, n, x, y);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (b </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> d)  </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    e </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> b </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> d </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> x </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> n </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> n;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> e </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> (n </span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> d);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main()<br><img id=Codehighlighter1_565_923_Open_Image onclick="this.style.display='none'; Codehighlighter1_565_923_Open_Text.style.display='none'; Codehighlighter1_565_923_Closed_Image.style.display='inline'; Codehighlighter1_565_923_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_565_923_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_565_923_Closed_Text.style.display='none'; Codehighlighter1_565_923_Open_Image.style.display='inline'; Codehighlighter1_565_923_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_565_923_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_565_923_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000"> a, b, c, ans;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> k;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> (scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%lld %lld %lld %d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">a, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">b, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">c, </span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">k) </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_671_906_Open_Image onclick="this.style.display='none'; Codehighlighter1_671_906_Open_Text.style.display='none'; Codehighlighter1_671_906_Closed_Image.style.display='inline'; Codehighlighter1_671_906_Closed_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_671_906_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_671_906_Closed_Text.style.display='none'; Codehighlighter1_671_906_Open_Image.style.display='inline'; Codehighlighter1_671_906_Open_Text.style.display='inline';" src="http://m.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_671_906_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://m.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_671_906_Open_Text><span style="COLOR: #000000">{<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (a </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&&</span><span style="COLOR: #000000"> b </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&&</span><span style="COLOR: #000000"> c </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&&</span><span style="COLOR: #000000"> k </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        ans </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> modular_linear_equation(c, b </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> a, 1LL </span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000"> k);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (ans </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">FOREVER</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%lld\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, ans);<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://m.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://m.shnenglu.com/Images/OutliningIndicators/None.gif" align=top></span></div> <img src ="http://m.shnenglu.com/sdfond/aggbug/76893.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/sdfond/" target="_blank">sdfond</a> 2009-03-17 18:53 <a href="http://m.shnenglu.com/sdfond/archive/2009/03/17/76893.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>{法求素?/title><link>http://m.shnenglu.com/sdfond/archive/2009/03/16/76775.html</link><dc:creator>sdfond</dc:creator><author>sdfond</author><pubDate>Mon, 16 Mar 2009 13:29:00 GMT</pubDate><guid>http://m.shnenglu.com/sdfond/archive/2009/03/16/76775.html</guid><wfw:comment>http://m.shnenglu.com/sdfond/comments/76775.html</wfw:comment><comments>http://m.shnenglu.com/sdfond/archive/2009/03/16/76775.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://m.shnenglu.com/sdfond/comments/commentRss/76775.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/sdfond/services/trackbacks/76775.html</trackback:ping><description><![CDATA[q个是经典的Eraosthenes{法Q?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">2</span><span style="color: #000000">; i </span><span style="color: #000000">*</span><span style="color: #000000"> i </span><span style="color: #000000"><</span><span style="color: #000000"> N; i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000"> (tag[i]) </span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br />    </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> i; i </span><span style="color: #000000">*</span><span style="color: #000000"> j </span><span style="color: #000000"><</span><span style="color: #000000"> N; j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />        tag[i</span><span style="color: #000000">*</span><span style="color: #000000">j] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br />}<br /></span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">2</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> N; i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #000000">tag[i])<br />        prime[tol</span><span style="color: #000000">++</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> i;<br /></span></div> <br />但是Eraosthenes{法的速度q不快,原因在于对于一个合敎ͼq种Ҏ(gu)?x)重复的标记。一U线性筛素数的方法有效的解决?jin)这一点,代码如下Q?br /> <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><span style="color: #0000ff">void</span><span style="color: #000000"> get_prime()<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> cnt </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />    </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">2</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> N; i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />    {<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #000000">tag[i])    p[cnt</span><span style="color: #000000">++</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> i;<br />        </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; j </span><span style="color: #000000"><</span><span style="color: #000000"> cnt </span><span style="color: #000000">&&</span><span style="color: #000000"> p[j] </span><span style="color: #000000">*</span><span style="color: #000000"> i </span><span style="color: #000000"><</span><span style="color: #000000"> N; j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />        {<br />            tag[i</span><span style="color: #000000">*</span><span style="color: #000000">p[j]] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br />            </span><span style="color: #0000ff">if</span><span style="color: #000000"> (i </span><span style="color: #000000">%</span><span style="color: #000000"> p[j] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">)<br />                </span><span style="color: #0000ff">break</span><span style="color: #000000">;<br />        }<br />    }<br />}</span></div> 可以用均摊分析的Ҏ(gu)来分析这个算法的复杂度:(x)׃每个合数都唯一的被它的最素因子{一ơ,而每个合数的最素因子都是唯一的,因此d杂度是O(n)?br />q种{法的思想很强(zhn),有很多利用h(hun)|可以Ҏ(gu)q种Ҏ(gu)做到U性筛Ƨ拉函数{等Q(h)l研I中。?img src ="http://m.shnenglu.com/sdfond/aggbug/76775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/sdfond/" target="_blank">sdfond</a> 2009-03-16 21:29 <a href="http://m.shnenglu.com/sdfond/archive/2009/03/16/76775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>整数快速幂取模http://m.shnenglu.com/sdfond/archive/2009/03/15/76625.htmlsdfondsdfondSun, 15 Mar 2009 02:58:00 GMThttp://m.shnenglu.com/sdfond/archive/2009/03/15/76625.htmlhttp://m.shnenglu.com/sdfond/comments/76625.htmlhttp://m.shnenglu.com/sdfond/archive/2009/03/15/76625.html#Feedback4http://m.shnenglu.com/sdfond/comments/commentRss/76625.htmlhttp://m.shnenglu.com/sdfond/services/trackbacks/76625.htmluva 374 Big Mod

赤裸裸的整数快速幂取模
#include <cstdio>

long long PowerMod(long long a, int b, int k)
{
    
long long tmp = a, ret = 1;
    
while (b)
    {
        
if (b & 1)
            ret 
= ret * tmp % k;
        tmp 
= tmp * tmp % k;
        b 
>>= 1;
    }
    
return ret;
}

int main()
{
    
long long a;
    
int b, k;

    
while (scanf("%lld %d %d"&a, &b, &k) == 3)
        printf(
"%lld\n", PowerMod(a, b, k));

    
return 0;
}


sdfond 2009-03-15 10:58 发表评论
]]>
žžƷ99þþ㽶| þƵ| þþž޾Ʒ| 91Ʒþþþþ91| þùҹaӰԺ | þþþòҰ¸߳| AVþüįٸ| 99þùۺϾƷӰԺ| þˬˬƬAV鶹| ޹Ʒľþþ| þ޾ƷAV| &#228;v뾫Ʒþþ | þ99ۺϾƷ| ѾƷþþþþĻ | ŷ޾þþþƷ| þ99Ʒþþþ| þþܳ| 99þþƷһëƬ| ˾þۺϳ| þþþ99ƷƬţţӰ | þоƷ| þۺۺϾþúݺݺ97ɫ88| vaĻþ| þþþþùaѹۿ| þþþ޾Ʒ| ŷպþAV| ƷŮþþþAV| 91þþƷһ| av˾þۺɫ | ޾ƷƷþ99| ƷþþӰ㽶| þ96Ʒþþ| 99þóĻ| ŮþþŮ| ŷպƷþþþ| ޾þһ| aaþʦ2021Ʒ | Ʒþþþþ99| 99þۺϾƷ | ĻþþƷ| ŷþۺ|