北邮OJ 2016 网预-Square Coins
時(shí)間限制?1000 ms?內(nèi)存限制?65536 KB
題目描述
? ? Artoria, also known as Saber-chan, was born into a time of chaos and war that began with the demise of the Roman empire. Somewhere in the far east, people in Utopia know nothing about war or conflicts. They live in peace for quite a long time and developed a strange currency system. In particular, they use square coins. Not only have they square shapes but also their values are square integers. Coins with values of all square numbers up to 289?(=172), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Utopia.
? ? According to the Utopia currency system, there are four combinations of coins to pay ten credits:
? ? ten 1-credit coins,
? ? one 4-credit coin and six 1-credit coins,
? ? two 4-credit coins and two 1-credit coins, and
? ? one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Utopia. The answer may be very big, please output the answer module?1000000009.
輸入格式
The input begins with a line containing a single integer?T(1≤T≤2000), indicating the number of test cases. Each of the next?T?lines each containing an integer meaning an amount to be paid. You may assume that all the amounts are positive and less than?2000.?
輸出格式
? ? For each of the given amount, output one line containing a single integer representing the number of combinations of coins module?1000000009. No other characters should appear in the output.
輸入樣例
2 10 30輸出樣例
4 27 物品有17種,無(wú)限取的容量有上限的背包,要求的是填滿時(shí)候有多少種裝法。不難推出狀態(tài)方程:
for(i=1;i<=17;i++) ????for(j=0;j<=n;j++) ????????dp[j+i*i]=(dp[j]+dp[j+i*i])%mod;dp[j]表示湊出j元錢(qián)的時(shí)候,有多少種湊法。
dp[j+i*i]表示,現(xiàn)在已經(jīng)有了j元錢(qián),我用i*i面額的貨幣可以得到j(luò)+i*i元錢(qián),那就是用湊出j元的方法數(shù)dp[j]加上已有的湊出j+i*i元錢(qián)的方法數(shù)dp[j+i*i]再模運(yùn)算一下即可。
答案即為dp[n]
#include<bits/stdc++.h> #define N 200010 #define ll long long #define M(x,y) x>y?x:y #define mod1 1000000009 #define clr0(x) memset(x,0,sizeof(x)) using namespace std; int main(){int n,t,ts,i,j,k,dp[5010];scanf("%d",&t);for(ts=1;ts<=t;ts++){int res=1;clr0(dp);scanf("%d",&n);dp[0]=1;for(i=1;i<=17;i++){for(j=0;j<=n;j++){dp[j+i*i]=(dp[j]+dp[j+i*i])%mod1;}}printf("%d\n",dp[n]);}return 0; }
總結(jié)
以上是生活随笔為你收集整理的北邮OJ 2016 网预-Square Coins的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 北邮OJ 2016网预 - Saber'
- 下一篇: 正整数的打印