而应该 在计算的中途就模除10^9,即 (a * b) % c == (a%c * b%c) % c。
快速幂就行了
BTW:
我的思路就是直接(n^2+1)*n^2/2
写错了吧,我怎么觉得应该是 (2^n+1) * 2^n / 2#include <stdio.h> // 计算 (a^b)%1000000007 unsigned quickpow( unsigned a, unsigned b ) { unsigned result = 1; for( unsigned t=a; b; b/=2 ) { if( b%2 == 1 ) result = (result*1ull*t)%1000000007; t = (t*1ull*t)%1000000007; } return result; } int main( void ) { unsigned n; scanf( "%u", &n ); // 计算 (2^n+1) * 2^n / 2 unsigned t = quickpow( 2, n-1 ); unsigned r = unsigned r = ((2*t+1ull)*t)/1000000007; printf( "%u\n", r ); }
[此贴子已经被作者于2018-9-30 14:22编辑过]