标签:
2 1 2
Case #1: 2 Case #2: 2
告诉一个数字n,求完全平方数摸2^n有多少不同的结果。没有说完全平方数的范围,想的话应该是循环着的,打表看一看,会发现n分奇数偶数时是有规律的。
n为奇数 2+(4^n-1)/3
n为偶数 2+2/3*(4^(n/2-1)-1)
算除法时分子很大,所以要用到逆元,求逆元的可以参看这篇 点击打开链接
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
typedef long long ll;
const int mod=10007;
using namespace std;
ll fun(ll n,ll m)
{
ll b=1;
while(m)
{
if(m&1) b=b*n%mod;
n=n*n%mod;
m>>=1;
}
return b;
}
int main()
{
int T;
ll n;
ll tmp=fun(3,mod-2);
scanf("%d",&T);
for(int ca=1;ca<=T;ca++)
{
scanf("%lld",&n);
ll ans;
if(n%2)
{
ll t=(fun(4,n/2)-1)%mod;
ans=(2+t*tmp%mod)%mod;
}
else
{
ll t=(fun(4,n/2-1)-1)%mod;
ans=(2+2*t*tmp%mod)%mod;
}
printf("Case #%d: %lld\n",ca,ans);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu 3524 Perfect Squares 推公式求逆元
标签:
原文地址:http://blog.csdn.net/wust_zjx/article/details/48113515