Little Sub has just received an equation, which is shown below, as his birthday gift.
Given the value of , please help Little Sub count the number of x(1<=x<=2^p)
which satisfies the equation.
Input
There are multiple test cases. The first line of the input contains an integer T (about 1000), indicating the number of test cases. For each test case:
The first and only line contains two integers a and p (1<=a<=10^9, 1<=p<=30).
Output
For each test case output one line containing one integer, indicating the answer.
Sample Input
2
6 12
8 16
Sample Output
1023
16383
题意:给你一个式子,让你求出在【1,2^p】中满足式子中的个数。(这是17年广西邀请赛原题,gg )(hdu6189)
思路:有点麻烦写到纸上了,上图。
这是当a为奇数时打表的函数和数据 :
AC代码:
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll quickmul(ll a,ll b,ll mod)
{
ll temp=a,ans=1;
while(b)
{
if(b&1)
ans=(ans*temp)%mod;
temp=(temp*temp)%mod;
b>>=1;
}
return ans;
}
void init()
{
ll ans=0;
ll mod=1<<20;
for(int a=1;a<=50;a++)
{
for(int i=1;i<=20;i++)
{
if(quickmul(a,i,mod)%mod==quickmul(i,a,mod)%mod)
{
ans++;
}
}
if(a&1) cout<<"奇数"<<endl;
cout<<ans<<endl;
ans=0;
}
}
int main()
{
// init();
ll t;
cin>>t;
while(t--)
{
ll a,p;
cin>>a>>p;
if(a&1)
{
cout<<"1"<<endl;
continue;
}
else
{
ll ans=0;
ll mod=1<<p;
for(int i=1;i<=p;i++)
{
if(quickmul(a,i,mod)%mod==quickmul(i,a,mod)%mod)
{
ans++;
}
}
ll q1;
q1=p/a;
if(p%a)
q1++;
ll l=1<<q1;
ans+=(mod/l-p/l);
cout<<ans<<endl;
}
}
return 0;
}