【模板库】拉格朗日插值法

参考例题:http://oi.cyo.ng/?p=3191

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 200;
const int MOD = 1000000007;

int n,m,K,r[N],u[N],f[N],g[N],h[N],alpha[N],C[N][N]; 

inline int read() {
	char c=getchar(); int f=1,ret=0;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret * f;
} 

inline int Pow(int w, int t) {
	int ret = 1;
	for (;t;t>>=1,w=(LL)w*w%MOD) {
		if (t & 1) {
			ret = (LL)ret * w % MOD;
		} 
	}
	return ret;
}

inline int LagrangePolynomial(int x, int len, int *ff, int *xx) {
	int ret = 0;
	for (int i=1;i<=len;i++) {
		int tmp = ff[i];
		for (int j=1;j<=len;j++) {
			if (i == j) continue;
			tmp = (LL)tmp * (x - xx[j]) % MOD;
			tmp = (LL)tmp * Pow(xx[i] - xx[j], MOD-2) % MOD;
		}
		ret = (ret + tmp) % MOD;
	}
	return (ret + MOD) % MOD;
} 

int main() {
	n = read(); m = read(); K = read();
	for (int i=1;i<=m;i++) {
		u[i] = read();
	}
	for (int i=1;i<=m;i++) {
		r[i] = read();
	}
	//预处理组合数 
	C[0][0] = 1;
	for (int i=1;i<=n;i++) {
		C[i][0] = 1;
		for (int j=1;j<=i;j++) {
			C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD;
		}
	}
	//拉格朗日插值
	for (int w=1;w<=m;w++) {
		for (int i=1;i<=n+1;i++) {
			f[i] = 0; h[i] = i;
			for (int j=1;j<=i;j++) {
				f[i] = (f[i] + (LL)Pow(i-j, r[w]-1) * Pow(j, n-r[w])) % MOD;
			}
		}  
		g[w] = LagrangePolynomial(u[w], n+1, f, h);
	}
	//广义容斥原理 
	int ans = 0;
	for (int i=K,t=1;i<=n;i++,t*=-1) {
		alpha[i] = C[n-1][i];
		for (int j=1;j<=m;j++) {
			alpha[i] = (LL)alpha[i] * C[n-1-i][r[j]-1] % MOD * g[j] % MOD;
		}
		ans = (ans + t * (LL)C[i][K] * alpha[i]) % MOD;
	}
	printf("%d\n",(ans+MOD)%MOD);
	return 0;
}

212 thoughts to “【模板库】拉格朗日插值法”

  1. Do you mind if I quote a couple of your posts as long as I
    provide credit and sources back to your website?

    My blog is in the very same area of interest as
    yours and my visitors would definitely benefit from a lot of the information you provide here.
    Please let me know if this alright with you. Appreciate it!

  2. Pretty section of content. I just stumbled upon your
    blog and in accession capital to assert that I
    get actually enjoyed account your weblog posts. Any way I’ll be
    subscribing for your feeds and even I fulfillment you access consistently rapidly.

  3. I was wondering if you ever thought of changing the structure of your blog?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of
    content so people could connect with it better. Youve got an awful lot of text for only
    having one or two pictures. Maybe you could
    space it out better?

  4. Hello! Do you know if they make any plugins to help with Search Engine
    Optimization? I’m trying to get my blog to rank for some targeted
    keywords but I’m not seeing very good success. If you know of any please share.

    Thank you!

  5. Hi there to every one, the contents existing at this website
    are genuinely remarkable for people experience, well, keep
    up the nice work fellows. pof natalielise

  6. You actually make it seem so easy with your presentation but I find this topic to be really
    something which I think I would never understand.
    It seems too complex and extremely broad for me.
    I’m looking forward for your next post, I will try to get the hang of it!
    natalielise plenty of fish

  7. Terrific post however , I was wondering if you could write
    a litte more on this subject? I’d be very grateful if you could elaborate a
    little bit more. Bless you!

  8. naturally like your web-site but you have to test the
    spelling on several of your posts. Many of them are rife with spelling
    problems and I in finding it very troublesome to tell the truth nevertheless I’ll surely come again again.

  9. I am really loving the theme/design of your site.
    Do you ever run into any internet browser compatibility issues?
    A number of my blog visitors have complained about my website not operating correctly in Explorer but looks great
    in Firefox. Do you have any ideas to help fix this issue?

  10. My spouse and I absolutely love your blog and find nearly
    all of your post’s to be exactly what I’m looking for.
    Would you offer guest writers to write content for you?
    I wouldn’t mind writing a post or elaborating on most of the subjects you write regarding here.
    Again, awesome web log!

  11. Hey there would you mind sharing which blog
    platform you’re using? I’m going to start my own blog soon but I’m having
    a hard time selecting between BlogEngine/Wordpress/B2evolution and
    Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something unique.
    P.S Apologies for being off-topic but I had to ask!

  12. Simply want to say your article is as surprising. The clarity in your
    post is simply great and i can assume you are an expert on this subject.
    Well with your permission allow me to grab your feed
    to keep up to date with forthcoming post. Thanks a million and please continue the rewarding work.

  13. Hello, i think that i saw you visited my web site thus i came to “return the
    favor”.I am trying to find things to enhance my site!I suppose its ok to use
    some of your ideas!!

  14. Hey I know this is off topic but I was wondering if you knew of any
    widgets I could add to my blog that automatically tweet my
    newest twitter updates. I’ve been looking for a plug-in like this
    for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog
    and I look forward to your new updates.

  15. I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my opinion, if all website owners and
    bloggers made good content as you did, the web will be much
    more useful than ever before.

  16. Hi there, I found your website via Google
    even as searching for a similar matter, your web site got here up, it
    seems to be good. I’ve bookmarked it in my google bookmarks.

    Hello there, just became aware of your blog via Google,
    and located that it’s truly informative. I am going to be careful for brussels.
    I will appreciate in case you proceed this in future.
    Many folks shall be benefited out of your writing. Cheers!

  17. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter
    updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have
    some experience with something like this. Please let me
    know if you run into anything. I truly enjoy reading your blog and I
    look forward to your new updates.

  18. Greetings from Los angeles! I’m bored to death at work so I decided to check out your site on my iphone during lunch break.
    I enjoy the information you present here and can’t wait to take
    a look when I get home. I’m amazed at how fast your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyways, amazing blog!

  19. Hmm it looks like your website ate my first
    comment (it was super long) so I guess I’ll just sum it up what I submitted
    and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to everything.
    Do you have any helpful hints for rookie blog writers? I’d really appreciate it.

  20. It’s a shame you don’t have a donate button! I’d certainly donate to this brilliant blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this website with my Facebook group. Talk soon!

  21. Hi there, I found your website via Google whilst searching for a
    related topic, your site got here up, it seems to be great.
    I have bookmarked it in my google bookmarks.

    Hello there, just become aware of your blog via Google, and located
    that it’s really informative. I am gonna watch out for brussels.
    I will be grateful when you proceed this in future.
    Many other people might be benefited from your writing.
    Cheers!

  22. Hmm is anyone else having problems with the pictures on this
    blog loading? I’m trying to figure out if its a problem on my
    end or if it’s the blog. Any feedback would be greatly appreciated.

  23. Order health insurers as much as you need, all sizes and quantities are available. Both forms, brand name and generic, are there to choose from. Now buying your medicines is as easy as 1-2-3. The convenience that you will see viagra online canadian pharmacy online is really wonderful.

  24. I do accept as true with all of the ideas you’ve offered in your post. They are very convincing and will definitely work. Still, the posts are very brief for newbies. May just you please lengthen them a little from subsequent time? Thank you for the post.

  25. I love your blog.. very nice colors & theme.

    Did you design this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to construct
    my own blog and would like to find out where u got this from.
    many thanks

  26. Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you
    would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I
    look forward to your new updates.

  27. This is the right website for anyone who really wants to understand this topic.

    You realize a whole lot its almost hard to argue with you (not that I really will
    need to…HaHa). You definitely put a brand new spin on a subject which has been discussed
    for years. Great stuff, just excellent!

  28. We are a gaggle of volunteers and starting a brand new scheme in our community.
    Your web site provided us with useful info to work on. You’ve performed
    an impressive job and our entire community might be thankful to you.

  29. Very nice post. I simply stumbled upon your blog and wanted to say
    that I’ve really enjoyed surfing around your weblog posts.
    In any case I will be subscribing for your rss feed and I hope you write once more soon!

  30. Howdy just wanted to give you a brief heads up and let you
    know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the
    same outcome.

  31. A motivating discussion is worth comment. I think that you ought to publish more about this issue, it may not be a
    taboo subject but typically people do not speak about such
    subjects. To the next! Kind regards!!

  32. Superb blog! Do you have any suggestions for aspiring writers?
    I’m hoping to start my own website soon but I’m a little
    lost on everything. Would you advise starting with a free platform like WordPress or go for
    a paid option? There are so many options out there that
    I’m totally overwhelmed .. Any recommendations?

    Thanks!

  33. Heya i am for the first time here. I found this board and I find It really useful & it helped me out a lot.
    I hope to give something back and help others like you aided me.

  34. My partner and I stumbled over here from a different website and thought I might as
    well check things out. I like what I see so i am just following you.

    Look forward to looking over your web page yet again.

  35. Admiring the persistence you put into your site and detailed information you present.
    It’s nice to come across a blog every once in a while that isn’t the same out of date rehashed information. Great read!
    I’ve bookmarked your site and I’m adding your RSS feeds to
    my Google account.

  36. My spouse and I absolutely love your blog and find the majority of
    your post’s to be exactly I’m looking for. Do you offer
    guest writers to write content for you? I wouldn’t mind producing a post or elaborating on a few of the subjects you write in relation to here.
    Again, awesome website!

  37. Definitely imagine that that you said. Your favorite reason appeared to
    be at the internet the easiest thing to understand of.

    I say to you, I definitely get irked at the same time as
    other folks consider issues that they just don’t recognise about.

    You controlled to hit the nail upon the top and also defined out the
    whole thing with no need side-effects , other people could take a signal.
    Will probably be again to get more. Thanks

  38. Good day! This is kind of off topic but I need some
    advice from an established blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about making my own but I’m not sure where to start.
    Do you have any tips or suggestions? Appreciate it

  39. First of all I want to say fantastic blog!

    I had a quick question in which I’d like to ask if you do not mind.
    I was curious to know how you center yourself and clear
    your head before writing. I’ve had a hard time clearing my thoughts in getting my
    ideas out. I truly do enjoy writing but it just seems like the first 10 to 15 minutes are lost just
    trying to figure out how to begin. Any ideas or hints?
    Thanks!

  40. Outstanding post however , I was wondering if you could write a litte more on this topic?
    I’d be very grateful if you could elaborate a little bit more.
    Appreciate it!

  41. There will always be someone who will accompany you to fulfill your unfulfilled wishes, send you flowers, accompany you to watch the sunrise and sunset, accompany you to walk the Yangtze River Bridge to accompany you to play the carousel, and spend the rest of your life with you.

  42. I like the valuable info you provide on your articles.
    I will bookmark your blog and test again right
    here frequently. I’m somewhat sure I’ll be told lots of new stuff proper right
    here! Good luck for the next!

  43. I will immediately grab your rss as I can not find your email subscription hyperlink or e-newsletter service.
    Do you’ve any? Please permit me recognize in order that I could subscribe.
    Thanks.

  44. This design is spectacular! You most certainly know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Wonderful job. I really loved
    what you had to say, and more than that, how you presented it.
    Too cool!

Leave a Reply

Your email address will not be published. Required fields are marked *