C++ Xor相等(codechef 5月21日挑战)对于某些值,例如n=4589,我得到了错误的结果。感谢您的帮助:)

C++ Xor相等(codechef 5月21日挑战)对于某些值,例如n=4589,我得到了错误的结果。感谢您的帮助:),c++,xor,modulo,C++,Xor,Modulo,/*对于给定的N,找到从范围[0,2N]中选择整数x的方法的数量−1] 这样x⊕(x+1)=(x+2)⊕(x+3),其中⊕ 表示按位异或运算符 由于有效x的数量可能很大,因此以109+7的模输出*/ #include <iostream> using namespace std; #define ll long long const ll N = 1e5; //user can input n upto 1e5 unsigned ll arr[N]; unsigned

/*对于给定的N,找到从范围[0,2N]中选择整数x的方法的数量−1] 这样x⊕(x+1)=(x+2)⊕(x+3),其中⊕ 表示按位异或运算符

由于有效x的数量可能很大,因此以109+7的模输出*/

#include <iostream>

using namespace std;
#define ll long long

const ll N = 1e5;      //user can input n upto 1e5
unsigned ll  arr[N];
unsigned ll p = 1e9 + 7;  // we have to find answer modulo p

ll mod_pow(ll a, ll b)
{
    if (b == 1)
    {
        return a;
    }
    ll   c   =   1;
    if  ( b % 2 == 0)
      c= ( mod_pow ( a  , b / 2) ) % p ; 
    else
    {
        c = ( mod_pow(a, (b - 1) / 2)) % p;
        return ((a % p) * c * c) % p;
    }
    return (c * c)%p;
}


 void pre()
{
    for ( unsigned ll i = 0;i < N;i++)
    {
        arr[i] = ( ( ( ( mod_pow ( 2, i+1) -1 + p ) %p ) * 1/2 ) %p + 1 ) %p;  
                                                  
                       / / precomputing for all even number in (2^n -1) 
    }

}
int main()
{
    ios::sync_with_stdio(false);cin.tie(NULL);
    pre();
    int t;
    cin >> t;

while (t--)
{
    int n;
    cin >> n;
    cout << (arr[n-1])<<endl ;
}

return 0;
}
#包括
使用名称空间std;
#定义ll long long
常数N=1e5//用户最多可输入1e5
未签名的ll-arr[N];
无符号ll p=1e9+7;//我们必须找到模p的答案
ll mod_pow(ll a、ll b)
{
如果(b==1)
{
返回a;
}
llc=1;
如果(b%2==0)
c=(模功率(a,b/2))%p;
其他的
{
c=(mod_pow(a,(b-1)/2))%p;
回报率((a%p)*c*c)%p;
}
返回值(c*c)%p;
}
void pre()
{
for(无符号ll i=0;i>t;
而(t--)
{
int n;
cin>>n;

实际上,解决方案只是二的幂:
answer=2^{n-1}

为了提高效率,这意味着最好首先迭代计算所有解决方案:

powerof2[n] = (2 * powerof2[n-1]) % p
#包括
constexpr int N=1e5;//用户最多可以输入1e5
无符号长arr[N];
constexpr unsigned int p=1e9+7;//我们必须找到模p的答案
//预先计算2的幂
void pre(){
arr[0]=1;
对于(int i=1;i>t;
而(t--){
int n;
标准:cin>>n;

std::你能提供一个问题的链接吗?并解释一下你使用的是什么算法。因为它只适用于我试图计算的偶数((2^N)-1)/2%1e9+7+1作为0也包括在内。我在大多数测试用例中都得到了正确的答案,但它并不满足一些测试用例,我发现其中一个是4589,即n=4589/你取模,然后除以2再取模。这可能是错误的。使用
1/2=(p+1)/2 mod p