1. 程式人生 > >週中訓練筆記+HDU5478Can you find it【快速冪】

週中訓練筆記+HDU5478Can you find it【快速冪】

去圖書館借書,找到數論那一架子,空了一大片了,看了半天沒找到想找的,跳來跳去,找了兩本組合數學先湊合著,不大沾邊,等大佬們看完,找他們借一下吧。

陷入大素數判定無法自拔ing

即將斷電,簡寫,下面這個題題意:給你一個c,k1,b1,k2求滿足下面式子的a,b

思路:這個題目我在網上看了幾波解釋,都是草草的一句只需要判斷n-1

和n=2時候就行了,沒什麼解釋,這幾天腦子不是很靈活,想了很久為什麼

要想等式成立,首先n=1時得成立。b=c-ppow(a,k1+b1,c);.

對於,每個n成立,則n+1時,式子只是前半項成上ppow(a,k1,c),後半項ppow(b,k2,c),要想原式子仍然成立,

則必須滿足ppow(a,k1,c)==ppow(b,k2,c),細想便可明白,只有乘上的係數相同才可能出現modc還是0

所以直接暴力解決該題

還有就是驗證n=1和n=2時成立則成立的,思想就這樣,重點是怎麼來的,其實和我方才說的一模一樣,每次只不過是

前半項成上ppow(a,k1,c),後半項ppow(b,k2,c)

如果n=2成立,後面再怎麼乘也還是成立啊

同樣是暴力列舉a 的值只是judge等式成立的方式不一樣

Can you find it

Given a prime number C(1C2×105)C(1≤C≤2×105), and three integers k1, b1, k2 (
1k1,k2,b1109)
(1≤k1,k2,b1≤109)
. Please find all pairs (a, b) which satisfied the equation ak1n+b1ak1⋅n+b1 + bk2nk2+1bk2⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, ...). InputThere are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2. OutputFirst, please output "Case #k: ", k is the number of test case. See sample output for more detail. 
Please output all pairs (a, b) in lexicographical order. (
1a,b<C)
(1≤a,b<C)
. If there is not a pair (a, b), please output -1. Sample Input
23 1 1 2
Sample Output
Case #1:
1 22
#include<iostream>
#include<stdio.h>
#include<string.h>
#define ll long long
using namespace std;
ll ppow(ll a,ll b,ll mod)
{
    ll ans=1;
    ll base=a;
    while(b!=0)
    {
        if(b&1!=0)
            ans=ans*base%mod;
        base=base*base%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    int cas=1;
    int c,k1,b1,k2;
    while(scanf("%d%d%d%d",&c,&k1,&b1,&k2)!=EOF)
    {
        int flag=0;
        printf("Case #%d:\n",cas++);
        for(ll a=1;a<c;a++)
        {
            ll b=c-ppow(a,k1+b1,c);
            if(ppow(a,k1,c)==ppow(b,k2,c))
            {
                printf("%lld %lld\n",a,b);
                flag=1;
            }
        }
        if(flag==0)
            printf("-1\n");
    }
    return 0;
}