Given a directed graph with n nodes, labeled 0,1,⋯,n−1.
For each <i,j> satisfies 0≤i<j<n, there exists an edge from the i-th node to the j-th node, the capacity of which is i xor j.
Find the maximum flow network from the 0-th node to the (n-1)-th node, modulo 1000000007.
Input Format
Multiple test cases (no more than 10000).
In each test case, one integer in a line denotes n(2≤n≤1018).
Output Format
Output the maximum flow modulo 1000000007 for each test case.
样例输入
2
样例输出
1
题目大意:
给出一个网络,让你求最大流。网络建图方式为:从i到j,流量为i^j,要求i<j;
思路:
暴力打表观察规律,对于结果其二进制下意义每位的价值是固定的。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long int
ll sum[150];
const ll mod=1000000007;
long long q_mul( long long a, long long b, long long mod ) //快速计算 (a*b) % mod
{
long long ans = 0; // 初始化
while(b) //根据b的每一位看加不加当前a
{
if(b & 1) //如果当前位为1
{
b--;
ans =(ans+ a)%mod; //ans+=a
}
b /= 2; //b向前移位
a = (a + a) % mod; //更新a
}
return ans;
}
long long poww( long long a, long long b, long long mod ) //快速计算 (a^b) % mod
{
long long ans = 1; // 初始化
while(b)//根据b的每一位看乘不乘当前a
{
if(b & 1) //如果当前位为1
{
ans = q_mul( ans, a, mod ); //ans*=a
}
b /= 2; //b向前移位
a = q_mul( a, a, mod ); //更新a
}
return ans;
}
void init()
{
sum[1]=2;
sum[2]=2+5;
for(ll i=3;i<=62;i++)
{
sum[i]=sum[i-1]*2;sum[i]=(sum[i]%mod+mod)%mod;
sum[i]=sum[i]-(ll)poww(2,(i-2)*2,mod);sum[i]=(sum[i]%mod+mod)%mod;
sum[i]=sum[i]+(ll)poww(2,(i-1)*2,mod);sum[i]=(sum[i]%mod+mod)%mod;
// sum[i]=sum[i-1]*2-(ll)poww(2,(i-2)*2,mod)+(ll)poww(2,(i-1)*2,mod);
}
}
int main()
{
ll n;
init();
while(~scanf("%lld",&n))
{
n--;
ll ans=0;
ll first=0;
for(ll i=61;i>=0;i--)
{
if(((1ll<<i)&n)>0)
{
first++;
if(first>1)
{
ans+=sum[i+1];
ans=((ans)%mod+mod)%mod;
}
else
{
ans+=1;
for(ll j=1;j<=i;j++)
{
ans+=sum[j];ans=((ans)%mod+mod)%mod;
}
}
}
}
printf("%lld\n",((ans)%mod+mod)%mod);
}
}