51nod1820 Great Wall Tour

Title Description

He who has never been to the Great Wall is not a true man , but not everyone can easily boarded the Great Wall.
Because only unlock $ LCM (k ^ {2 ^ l} + 1, k ^ {2 ^ {(l + 1)}} + 1, ......, k ^ {2 ^ r} +1) $ This question people before they can climb. Wherein k, l, r is a given integer. As a result of the relatively large, we will take more than the result of p, p is a prime number. 
  Description:
$ LCM (A_1, A_2, ..., A_N) $ represents the $ a_1, a_2, ..., a_n $ of the least common multiple.
We require, for any integer x, $ x ^ 0 = 1 $;

He who has never been to the Great Wall is not a true man, but not everyone can easily boarded the Great Wall.

Because only unlock LCM (k2l + 1, k2 (l + 1) + 1, ......, k2r + 1) LCM (k2l + 1, k2 (l + 1) + 1, ......, k2r + 1)

 This question of people before they can climb. Wherein k, l, r is a given integer. As a result of the relatively large, we will take more than the result of p, p is a prime number. 

Description:

LCM(a1,a2,…,an)LCM(a1,a2,…,an)

 It indicates that a1, a2, ..., ana1, a2, ..., an

 The least common multiple.

We require, for any integer x, x0 = 1x0 = 1

 ;

** Collapse

** Expand text

**

Entry

`` `{.ng-binding style =" font-size: medium; word-wrap: break-word; white-space: pre-wrap; "}
plurality of test data sets:
a first row, we have an integer t (1 ≤t≤10 ^ 5) indicates how many test cases.

T next row, each row has four integer format "ki li ri pi" (1≤ki≤10 ^ 6,0≤li≤ri≤10 ^ 18,2≤pi≤10 ^ 9, to ensure that p is a prime number), the formula represents a casual working k, l, r, p.
`` `

Export

{.ng-binding style="font-size: medium; word-wrap: break-word; white-space: pre-wrap;"} 共t行,每行一个整数,表示对应的答案。

SAMPLE INPUT

2
3 1 10 2
5 0 4 3

Sample Output

0
0

**

BB

A blood lost (playing back games Comet OJ gone)

But later he brushes a water problem

answer

LCM + = modulo conclusion title

Conclusion 1

$ Gcd (k ^ {2 ^ i} + 1, k ^ {2 ^ j} +1) = 1 (i \ neq j and k is an even number) $

prove:

Provided i <j

If $ q \ mid k exists ^ {2 ^ i} + 1 $, the $ k ^ {2 ^ i} \ equiv -1 (mod; q) $

Then $ k ^ {2 ^ {i + k}} \ equiv 1 (mod; q) $ (k> 0), then the $ k ^ {2 ^ j} \ equiv 1 (mod; q) $, $ k ^ {2 ^ j} +1 \ equiv 2 (mod; q) $

When q> 2 no solution, due for q = 2 k is an even number, k + 1'd so power is odd, there is a factor of 2 (i.e., no solution)

So gcd = 1

Conclusion 2

gcd (k ^ {2 ^ i} + 1, k ^ {2 ^ j} +1) = 2 (i \ neq j and k is an odd number) $

prove:

Supra, can be found only common factor q = 2

Fucks

Simple and natural

First off special sentenced modulus 2

①K not a multiple of P

If K is not a multiple of P, then the formula becomes apart

$ans=\sum_{i=0}^{2^{r-l+1}-1}{({k^{2^l}})^i}$

Provided $ a = {k ^ {2 ^ l}} $, the $ ans = \ sum_ {i = 0} ^ {2 ^ {r-l + 1} -1} {a ^ i} $

2 ^ l ^ and $ 2 ^ r $ power can quickly find, because a ^ 0 ^ = a ^ p-1 ^ mod p = 1, can be found in fact modulus (P-1)

The rest is a geometric series summation

Because multiple K is not P and P is a prime number, the $ k ^ {2 ^ {i}} $ surely not 0, $ k ^ {2 ^ {i}} - 1 $ (geometric series summation of the denominator) not -1

But $ k ^ {2 ^ {i}} $ may be 1, so $ ans = 2 ^ {r-l + 1} $

P is a multiple of ②K

Obviously ans = 1


If K is an odd number, then you should get rid of the extra 2 ^ RL ^

code

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;

long long K,L,R,mod,Mod,S,ans;
int Q,i,j,k,l;

long long qpower(long long a,long long b)
{
    long long ans=1;
    
    while (b)
    {
        if (b&1)
        ans=ans*a%mod;
        
        a=a*a%mod;
        b>>=1;
    }
    
    return ans;
}

void js()
{
    long long s1,s2,S1,S2;
    
    --mod;
    s1=qpower(2,R+1);
    s2=qpower(2,L);
    
    s1-=s2;
    if (s1<0)
    s1+=mod;
    ++mod;
    
    S1=qpower(K,s1);
    S2=qpower(K,s2);
    
    if (S2>1)
    ans=(S1*S2%mod-1)*qpower(S2-1,Mod)%mod;
    else
    ans=qpower(2,R-L+1);
    
    if (ans<0)
    ans+=mod;
}

int main()
{
//  freopen("51nod_1820_4_in.txt","r",stdin);
//  freopen("51nod1820.in","r",stdin);
//  freopen("51nod1820.out","w",stdout);
    
    scanf("%d",&Q);
    for (;Q;--Q)
    {
        scanf("%lld%lld%lld%lld",&K,&L,&R,&mod);
        Mod=mod-2;
        
        if (mod==2)
        {
            if (K&1)
            printf("0\n");
            else
            printf("1\n");
            
            continue;
        }
        
        if (!(K%mod))
        ans=1;
        else
        {
            ans=0;
            js();
        }
        
        if (K&1)
        ans=ans*qpower((mod+1)/2,R-L)%mod;
        
        printf("%lld\n",ans);
    }
}

Guess you like

Origin www.cnblogs.com/gmh77/p/11483052.html