yandex资格赛C(dp->快速幂)

这场的bc都不太容易切啊 。。b的话还是考逻辑想象。。思维度没有。。

然后这个c还是蛮有意思。。

考虑一维情况很容易想到dp方程

设扫到第i个数前缀和余数为j的方案为d[i][j]

d[i][j]=sum(d[i-1][k])  k=0-n-1

       =n*d[i-1][j]

因为对上个余数k肯定能在1-n找到数使余数为 j。。所以余数为几基本都一样。。可以简化为:

d[i]*=n*d[i-1]*

求d[n][0]直接n^(n-1)即可。。

推广到二维。。

设扫到第i列j行前缀和余数为tj为d[i][t1][t2][t3]...[tn]

d[i][t1][t2][t3]...[tn]=sum(d[i-1][t1'][t2']..[tn'])

d[i-1]里面有n^n个状态供转移,但确不是d[i]*=n^n*d[i-1]*,因为多了个列和被n整除的情况,取数有限制,并不是所有的状态都能转移。。

所以该怎么办?换个想法。。如果随意取前n-1个数,那么第n个数一定是存在且确定的。。那这样我们将方程的维数降1即d[i][t1][t2]...[tn-1]。。这样n^(n-1)个状态就能完全转移。。。

d[i]*=n^(n-1)*d[i-1]*

就变成求n^(n-1)^(n-1)=n^((n-1)^2)了。。

然后直接快速幂。。。






/**
 *        ┏┓    ┏┓ 
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃   
 *        ┃   ━    ┃ 
 *        ┃ >   < ┃ 
 *        ┃       ┃ 
 *        ┃... ⌒ ...  ┃ 
 *        ┃       ┃ 
 *        ┗━┓   ┏━┛ 
 *          ┃   ┃ Code is far away from bug with the animal protecting           
 *          ┃   ┃   神兽保佑,代码无bug 
 *          ┃   ┃            
 *          ┃   ┃         
 *          ┃   ┃ 
 *          ┃   ┃            
 *          ┃   ┗━━━┓ 
 *          ┃       ┣┓ 
 *          ┃       ┏┛ 
 *          ┗┓┓┏━┳┓┏┛ 
 *           ┃┫┫ ┃┫┫ 
 *           ┗┻┛ ┗┻┛ 
 */  
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 205
#define nm 100005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1000000007;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}









ll n;
ll pow(ll x,ll t){return t==0?1:pow(sqr(x)%inf,t>>1)*(t&1?x:1)%inf;}

int main(){
	n=read();
	printf("%lld\n",pow(n,sqr(n-1)));
}




防止找不到原题贴个链接:

https://contest.yandex.com/algorithm2018/contest/7491/problems/C/

Beautiful Tables

Time limit1 second
Memory limit512Mb
Inputstandard input or input.txt
Outputstandard output or output.txt

Victor usually spends free time with reading books, solving riddles and puzzles.

Yesterday he decided to come up with his own puzzle. One should fill the table of size n × n with integers from 1 to n in a such way that the sum of the numbers in each row and the sum of numbers in each column is divisible by n. Each integer from 1 to n can be used arbitrary number of times.

Help Victor to determine the number of distinct tables satisfying the requirements of the puzzle. Two table are considered to be distinct if they differ in at least one cell. As the number Victor wants to compute may be pretty big, you only need to find its remainder modulo 109 + 7.

Input format

The only input line contains a single integer n (1 ≤ n ≤ 1000), the number of row and the number of columns of the table.

Output format

Print the number of appropriate tables modulo 1 000 000 007.

Sample 1

InputOutput
2
2

Sample 2

InputOutput
3
81

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值